0
votes

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!

3

3 Answers

2
votes

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
}
1
votes

I had this problem too and solved it using notifications. In your parent controller, in viewDidLoad method, you have to add an observer:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(closePopover)
                                                 name:@"closePopoverName"
                                               object:nil];

Then, in your viewDidUnload method you remove the observer like this:

[[NSNotificationCenter defaultCenter] removeObserver:self];

Of course, you have to create the function which actually dismisses the popover:

- (void) closePopover
{
    [_popover dismissPopoverAnimated:YES];
}

Then, in your ItemContent controller, you just post a notification when you want to close the popover:

[[NSNotificationCenter defaultCenter] postNotificationName:@"closePopoverName" object:self userInfo:nil];
0
votes

You could define a protocol on ItemContent and use it on your CallerViewController. If you want to close your Popover, just call your delegate method, which you will implement on your CallerViewController