Search code examples
iosobjective-cuipopovercontroller

UIPopoverController: update view after it is dismissed


On iPad simulator, I have a ViewController A that presents an UIPopoverController whose contentViewController is ViewController B, inside which I have a button to dismiss the UIPopoverController.

When it is dismissed, I need to update the view of ViewController A based on some field in ViewController B.

In order to do this, I am declaring ViewController A as a property (weakref) of ViewController B so that within ViewController B where it dismisses the popover, I can say:

[self.viewControllerA.popover dismissPopoverAnimated:YES];
self.viewControllerA.popover = nil;
self.viewControllerA.textLabel.text = self.someField

Is this the correct way of doing it? Since there is no callback when we dismiss the popover pragmatically, I can't think of any better solution.

Anybody has a better idea? Passing view controllers around just seems awkward to me.


Solution

  • The best way is use of Delegation, just declare the delegate in your controller B like

    @protocol ControllerSDelegate <NSObject>
    -(void) hidePopoverDelegateMethod;
    @end
    

    and call this on action for passing the data and dismiss of controller like

    if (_delegate != nil) {
        [_delegate hidePopoverDelegateMethod];
    }
    

    and

    in your controller A you can handle this delegate call

    -(void) hidePopoverDelegateMethod {
        [self.paymentPopover dismissPopoverAnimated:YES];
        if (self.paymentPopover) {
            self.paymentPopover = nil;
        }
        [self initializeData];
    }