Search code examples
iosobjective-cuinavigationcontrolleruipopovercontrollerpopover

Dismissing UIPopoverController with UINavigationController


I'm currently building an iPad app in which I need to implement a pop over view.

I have set up a view controller like I always do:

  • Create UIViewController wit xib file
  • set the xib up and do the necessary programming in it's .h & .m files

now in the view controller I'm loading it from (from a UIBarButtonItem), I have this code:

- (void) action
{
    ItemContent *newItem = [[ItemContent alloc] initWithNibName:@"ItemContent" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:newItem];
    _popover = [[UIPopoverController alloc] initWithContentViewController:nav];
    [_popover setPopoverContentSize:CGSizeMake(557, 700) animated:YES];
    _popover.delegate = self;
    [_popover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}

This properly displays my view- and UINavigationController in a UIPopOverController. So far so good!

In the newItem view controller, I have made a bar button in my navigation bar that says "Done". When that button is pushed, I want the UIPopOverController to disappear. How do I do this:

  • Set a method for when te button is pushed. In this method I want to call a function on the view controller that loaded the Popover to dismiss it again.. but how do I do this?

Put things shortly

How do I make my UIPopOverController call a method on the view controller that loaded the UIPopOverController?

I have been searching SO for a while but none of the solutions and answers solve my problem. If I missed something please inform me ;)

Thank you so much in advance!


Solution

  • You can do this by delegate... In NewItem.h declare a protocol

    @protocol NewItemDelegate
    
    -(void)onTapDoneButton;
    @end
    

    Now create a delegate property like this

    @property (nonatomic, assign) id<NewItemDelegate>delegate;
    

    In NewItem.m in doneButtonPuhsed method call this

    [self.delegate onTapDoneButton];
    

    Change this method a bit

    - (void) action
    {
        ItemContent *newItem = [[ItemContent alloc] initWithNibName:@"ItemContent" bundle:nil];
        newItem.delegate =self;
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:newItem];
        _popover = [[UIPopoverController alloc] initWithContentViewController:nav];
        [_popover setPopoverContentSize:CGSizeMake(557, 700) animated:YES];
        _popover.delegate = self;
        [_popover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    }
    

    Now implement NewItemDelegate method below this action method.

    -(void)onTapDoneButton{
    //dismiss popover here
    }