Skip to content

Commit

Permalink
Added PopoverManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ruegenberg committed Jun 4, 2011
1 parent 121461b commit 22dd89e
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
68 changes: 68 additions & 0 deletions SynthesizeSingleton.h
Original file line number Original file line Diff line number Diff line change
@@ -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; \
}
24 changes: 24 additions & 0 deletions UIKitUtils/PopoverManager.h
Original file line number Original file line Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions UIKitUtils/PopoverManager.m
Original file line number Original file line Diff line number Diff line change
@@ -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.