Search code examples
iphoneobjective-cipaduitableviewuipopover

UIPopover and UITableView data exchange


I have a UITableView in a UINavigationController. On the navigation bar I have a button called add. When this button is pressed it presents a UIPopoverController, where user can input data to be added as a new row/cell in the UITableView. My issue is how can I add a new cell to the UITableView from the UIPopover? Do I pass in the array data to the UIPopOver root controller?


Solution

  • There are two solutions to this that I'm aware of. One would be to send a notification from the popover to the root controller and apply the necessary code to update the tableView in the handleNotification method.

    The other, one that I personally use, is to set up a delegate protocol for the popover. You'll have to set it up something like this:

    @protocol PopoverDelegate
    - (void)addNewCell;  // you can add any information you need to pass onto this if necessary such as addNewCellWithName:(NSString *)name, etc.
    @end
    
    @interface MyPopoverViewController..... {
        id <PopoverDelegate> delegate;
        // the rest of your interface code;
    }
    @property (nonatomic, retain) id delegate;
    // any other methods or properties;
    @end
    

    Then in your root view controller header file, you need to add the delegate

    @interface RootViewController .... <PopoverDelegate> {
    

    Then in your root view controller implementation file, assign the popover delegate when you instantiate it. For example:

    MyPopoverViewController *vc = [[MyViewController alloc] init];
    vc.delegate = self;  // this is where you set your protocol delegate
    myPopover = [[UIPopoverController alloc] initWithContentViewController:vc];
    myPopover.delegate = self;
    [vc release];
    

    Finally, you'll add your protocol method somewhere in the code

    - (void)addNewCell {
        // do what you want with the tableView from here
    }
    

    Sorry that's a bit long. I just wanted to make sure I was thorough. Hope it helps