In your app delegate, have property for the current ViewController class:
1 2 3 4 5 6 7 8 9 |
@class ViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> { } @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) ViewController *viewController; @end |
Then in our didFinishLaunchingWithOptions method, we allocate this main ViewController, which we will insert into a newly created UINavigationController variable. We create this new UINavigationController below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch self.viewController = [[ViewController alloc] init]; //1) first, we must create a navigation controller UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; //2) then, assign it as the root view controller self.window.rootViewController = navController; [self.window makeKeyAndVisible]; return YES; } |
This way, our root is this navigation controller. And the navigation controller’s first page is our view controller.
View Controller
ViewController.h
1 2 3 4 5 6 7 8 9 10 |
@interface ViewController : UIViewController <ColorPickerDelegate> { } @property (nonatomic, strong) UIPopoverController *popController; - (void)showPopover:(id)sender; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIBarButtonItem *popoverButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPopover:)]; self.navigationItem.rightBarButtonItem = popoverButton; } - (void)showPopover:(id)sender { NSLog(@"show popover "); if (self.popController.popoverVisible) { NSLog(@"its visible, let's make it disappear"); [self.popController dismissPopoverAnimated:YES]; return; } //create the uiviewcontroller that will sit on top of the popover UIViewController *contentViewController = [[UIViewController alloc] init]; //background is yellow contentViewController.view.backgroundColor = [UIColor yellowColor]; UIPopoverController *popController = [[UIPopoverController alloc] initWithContentViewController:contentViewController]; popController.popoverContentSize = CGSizeMake(300.0f, 600.0f); self.popController = popController; [self.popController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; } |
Run the demo and you should have a yellow popover window appear.
Now let’s put a table that has 3 colors to choose from in there instead.
The key is that we create a viewcontroller that extends from UITableViewController. when the table view controller gets a click on a entry, it sends its corresponding UI uiviewcontroller messages via a delegate.
ColorPickerDelegate and ColorPickerTableViewController
1 2 3 4 5 6 7 8 9 10 |
@protocol ColorPickerDelegate <NSObject> @required -(void)selectedColor:(UIColor *)newColor; @end @interface ColorPickerTableViewController : UITableViewController {} @property (nonatomic, strong) NSMutableArray *colorNames; @property (nonatomic, weak) id<ColorPickerDelegate> delegate; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
- (id)initWithStyle:(UITableViewStyle)style { if ([super initWithStyle:style] != nil) { //Initialize the array colorNames = [NSMutableArray array]; //Set up the array of colors. [colorNames addObject:@"Red"]; [colorNames addObject:@"Green"]; [colorNames addObject:@"Blue"]; //Make row selections persist. self.clearsSelectionOnViewWillAppear = NO; } return self; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [colorNames count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell... cell.textLabel.text = [colorNames objectAtIndex:indexPath.row]; return cell; } #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *selectedColorName = [colorNames objectAtIndex:indexPath.row]; //Create a variable to hold the color, making its default //color something annoying and obvious so you can see if //you've missed a case here. UIColor *color = [UIColor orangeColor]; //Set the color object based on the selected color name. if ([selectedColorName isEqualToString:@"Red"]) { color = [UIColor redColor]; } else if ([selectedColorName isEqualToString:@"Green"]){ color = [UIColor greenColor]; } else if ([selectedColorName isEqualToString:@"Blue"]) { color = [UIColor blueColor]; } //Notify the delegate if it exists. if (delegate != nil) { [delegate selectedColor:color]; } } |
Go Back to your ViewController and change/add the following methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
- (void)showPopover:(id)sender { NSLog(@"show popover "); if (self.popController.popoverVisible) { NSLog(@"its visible, let's make it disappear"); [self.popController dismissPopoverAnimated:YES]; return; } //standard //UIViewController *contentViewController = [[UIViewController alloc] init]; //background is yellow // contentViewController.view.backgroundColor = [UIColor yellowColor]; //create the uitableViewcontroller NSLog(@"its NOT visible, let's make it appear"); ColorPickerTableViewController * contentViewController = [[ColorPickerTableViewController alloc] initWithStyle:UITableViewStylePlain]; contentViewController.delegate = self; UIPopoverController *popController = [[UIPopoverController alloc] initWithContentViewController:contentViewController]; popController.popoverContentSize = CGSizeMake(300.0f, 600.0f); self.popController = popController; [self.popController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; } #pragma mark - ColorPickerDelegate method -(void)selectedColor:(UIColor *)newColor { NSLog(@"got it"); if (self.popController.popoverVisible) { [self.popController dismissPopoverAnimated:YES]; return; } } |