Created
May 24, 2010 16:28
-
-
Save lukeredpath/412093 to your computer and use it in GitHub Desktop.
Singleton popover manager to enforce the one popover at at time rule. MIT license, help yourself
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LRPopoverManager.h | |
// Spark | |
// | |
// Created by Luke Redpath on 24/05/2010. | |
// Copyright 2010 LJR Software Limited. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
extern NSString *const LRUIPopoverControllerDidDismissNotification; | |
@interface LRPopoverManager : NSObject <UIPopoverControllerDelegate> { | |
UIPopoverController *currentPopoverController; | |
BOOL permitCurrentPopoverControllerToDismiss; | |
} | |
@property (nonatomic, retain) UIPopoverController *currentPopoverController; | |
@property (nonatomic, assign) BOOL permitCurrentPopoverControllerToDismiss; | |
+ (id)sharedManager; | |
- (void)presentPopoverController:(UIPopoverController *)pc fromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated; | |
- (void)presentPopoverController:(UIPopoverController *)pc fromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated; | |
- (void)presentControllerInPopoverController:(UIViewController *)vc fromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated; | |
- (void)presentControllerInPopoverController:(UIViewController *)vc fromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated; | |
- (void)dismissCurrentPopoverController:(BOOL)animated; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LRPopoverManager.m | |
// Spark | |
// | |
// Created by Luke Redpath on 24/05/2010. | |
// Copyright 2010 LJR Software Limited. All rights reserved. | |
// | |
#import "LRPopoverManager.h" | |
NSString *const LRUIPopoverControllerDidDismissNotification = @"LRUIPopoverControllerDidDismissNotification"; | |
@implementation LRPopoverManager | |
@synthesize currentPopoverController; | |
@synthesize permitCurrentPopoverControllerToDismiss; | |
static LRPopoverManager *sharedManager = nil; | |
+ (void)initialize { | |
if (self == [LRPopoverManager class]) { | |
sharedManager = [[self alloc] init]; | |
sharedManager.permitCurrentPopoverControllerToDismiss = YES; | |
} | |
} | |
+ (id)sharedManager { | |
return sharedManager; | |
} | |
- (void)setCurrentPopoverController:(UIPopoverController *)pc | |
{ | |
[self dismissCurrentPopoverController:YES]; | |
if (pc != currentPopoverController) { | |
[currentPopoverController release]; | |
currentPopoverController = [pc retain]; | |
currentPopoverController.delegate = self; | |
} | |
self.permitCurrentPopoverControllerToDismiss = YES; | |
} | |
- (void)presentPopoverController:(UIPopoverController *)pc fromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated | |
{ | |
self.currentPopoverController = pc; | |
[self.currentPopoverController presentPopoverFromRect:rect inView:view permittedArrowDirections:arrowDirections animated:animated]; | |
} | |
- (void)presentPopoverController:(UIPopoverController *)pc fromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated | |
{ | |
self.currentPopoverController = pc; | |
[self.currentPopoverController presentPopoverFromBarButtonItem:item permittedArrowDirections:arrowDirections animated:animated]; | |
} | |
- (void)dismissCurrentPopoverController:(BOOL)animated; | |
{ | |
[self.currentPopoverController dismissPopoverAnimated:animated]; | |
} | |
- (void)presentControllerInPopoverController:(UIViewController *)vc fromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated; | |
{ | |
UIPopoverController *pc = [[UIPopoverController alloc] initWithContentViewController:vc]; | |
[self presentPopoverController:pc fromRect:rect inView:view permittedArrowDirections:arrowDirections animated:animated]; | |
[pc release]; | |
} | |
- (void)presentControllerInPopoverController:(UIViewController *)vc fromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated; | |
{ | |
UIPopoverController *pc = [[UIPopoverController alloc] initWithContentViewController:vc]; | |
[self presentPopoverController:pc fromBarButtonItem:item permittedArrowDirections:arrowDirections animated:animated]; | |
[pc release]; | |
} | |
#pragma mark - | |
#pragma mark UIPopoverControllerDelegate methods | |
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController | |
{ | |
[[NSNotificationCenter defaultCenter] postNotificationName:LRUIPopoverControllerDidDismissNotification object:popoverController]; | |
} | |
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController | |
{ | |
return self.permitCurrentPopoverControllerToDismiss; | |
} | |
@end |
Do let me know (by Twitter, or on here) if you run into any problems with it or have any improvements.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome. I will definitely be using this.