Permalink
Browse files

Added PopoverManager.

  • Loading branch information...
1 parent 121461b commit 22dd89e5246e564fd480794dc79a3dce1ac5d281 Marcel Ruegenberg committed Jun 4, 2011
Showing with 142 additions and 0 deletions.
  1. +68 −0 SynthesizeSingleton.h
  2. +24 −0 UIKitUtils/PopoverManager.h
  3. +50 −0 UIKitUtils/PopoverManager.m
View
@@ -0,0 +1,68 @@
+//
+// SynthesizeSingleton.h
+// CocoaWithLove
+//
+// Created by Matt Gallagher on 20/10/08.
+// Copyright 2009 Matt Gallagher. All rights reserved.
+//
+// Permission is given to use this source code file without charge in any
+// project, commercial or otherwise, entirely at your risk, with the condition
+// that any redistribution (in part or whole) of source code must retain
+// this copyright and permission notice. Attribution in compiled projects is
+// appreciated but not required.
+//
+
+#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
+ \
+static classname *shared##classname = nil; \
+ \
++ (classname *)shared##classname \
+{ \
+ @synchronized(self) \
+ { \
+ if (shared##classname == nil) \
+ { \
+ shared##classname = [[self alloc] init]; \
+ } \
+ } \
+ \
+ return shared##classname; \
+} \
+ \
++ (id)allocWithZone:(NSZone *)zone \
+{ \
+ @synchronized(self) \
+ { \
+ if (shared##classname == nil) \
+ { \
+ shared##classname = [super allocWithZone:zone]; \
+ return shared##classname; \
+ } \
+ } \
+ \
+ return nil; \
+} \
+ \
+- (id)copyWithZone:(NSZone *)zone \
+{ \
+ return self; \
+} \
+ \
+- (id)retain \
+{ \
+ return self; \
+} \
+ \
+- (NSUInteger)retainCount \
+{ \
+ return NSUIntegerMax; \
+} \
+ \
+- (void)release \
+{ \
+} \
+ \
+- (id)autorelease \
+{ \
+ return self; \
+}
@@ -0,0 +1,24 @@
+//
+// PopoverManager.h
+// Lists
+//
+// Created by Marcel Ruegenberg on 25.02.11.
+// Copyright 2011 Dustlab. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+/**
+ A class for managing the retaining of popovers in a central place.
+
+ Because even though Apple basically tells you to have only one popover at a time, they make it surprisingly hard to do this properly.
+ */
+@interface PopoverManager : NSObject {
+ UIPopoverController *currentPopoverController;
+}
+
+@property (nonatomic, retain) UIPopoverController *currentPopoverController;
+
++ (PopoverManager *)sharedPopoverManager;
+
+@end
@@ -0,0 +1,50 @@
+//
+// PopoverManager.m
+// Lists
+//
+// Created by Marcel Ruegenberg on 25.02.11.
+// Copyright 2011 Dustlab. All rights reserved.
+//
+
+#import "PopoverManager.h"
+#import "SynthesizeSingleton.h"
+
+@implementation PopoverManager
+@synthesize currentPopoverController;
+
+SYNTHESIZE_SINGLETON_FOR_CLASS(PopoverManager);
+
+- (id)init {
+ if((self = [super init])) {
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
+ }
+ return self;
+}
+
+- (void)setCurrentPopoverController:(UIPopoverController *)thePopoverController {
+ if(thePopoverController != currentPopoverController) {
+ if(currentPopoverController && currentPopoverController.popoverVisible) {
+#ifndef NDEBUG
+ NSLog(@"Warning: New popover being set in PopoverManager while existing popover is still visible.");
+#endif
+ [currentPopoverController dismissPopoverAnimated:NO]; // no animation when dismissing because this should not happen
+ }
+ [currentPopoverController release];
+ currentPopoverController = [thePopoverController retain];
+ }
+}
+
+- (void)didReceiveMemoryWarning:(NSNotification *)notification {
+ if(self.currentPopoverController && ! self.currentPopoverController.popoverVisible) {
+ self.currentPopoverController = nil;
+ }
+}
+
+- (void) dealloc
+{
+ self.currentPopoverController = nil;
+ [super dealloc];
+}
+
+
+@end

0 comments on commit 22dd89e

Please sign in to comment.