Domanda

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.

È stato utile?

Soluzione

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];
}

Altri suggerimenti

I think, delegates or sending NSNotification will make better.

Note:

A change of the execution sequence will do more perfection to your current code.

self.viewControllerA.textLabel.text = self.someField
[self.viewControllerA.popover dismissPopoverAnimated:YES];
self.viewControllerA.popover = nil;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top