Domanda

I need to call a delegate method on my main view controller ('showDetails:') from a popover view's pushed view (embedded in navigation controller). This is all from a storyboard setup.

The hierarchy is: Main view -> Popover (menu tableview embedded in navigation controller)->Popover secondary View (pushed onto popover navigation controller)

I know how to setup a delegate on the popover using prepareForSegue, but not on an inner view. How can I call a delegate method on the main view from an inner (pushed) view of a popover?

Here is how I setup the delegate on a popover main view:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    
    if ([segue.identifier isEqualToString:@"segueSearchResults"]) {
        //Dismiss User Popover
        [self dismissUserPopover];

        SearchResultsViewController *vc = segue.destinationViewController;
        vc.searchDelegate = self;
        self.searchPopover = [(UIStoryboardPopoverSegue *)segue popoverController];
        self.searchPopover.delegate = self;

    }
}
È stato utile?

Soluzione

Instead Delegate i prefer "NSNotificationCenter" in your case

Add an observer to your ViewController for some action in uiview

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

Post Notification from your pushed View in PopOverController Post Notification and method in your Viewcontroller will be called

[[NSNotificationCenter defaultCenter] postNotificationName:@"someActionNotification" object:self];

At the end Dont forget to remove Observer.

[[NSNotificationCenter defaultCenter] removeObserver:@"someActionNotification"];

Altri suggerimenti

When you need to communicate between two view controllers which are far apart in the VC hierarchy, trying to reference one from the other so you can directly call methods on it doesn't work so well -- there's several levels of indirection in between, and it's very fragile if you change your VC hierarchy later.

Look into notifications (NSNotificationCenter) instead; you can have one VC "broadcast" info for another to respond to, regardless of where they are in your app.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top