0

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;

    }
}
Saqib Saud
  • 2,799
  • 2
  • 27
  • 41
JimmyJammed
  • 9,598
  • 19
  • 79
  • 146
  • I'm not sure whether you're using the term child view properly. Is this last controller a child view controller of the popover controller, or are you pushing to this last controller? – rdelmar Mar 24 '13 at 02:43
  • Sorry, yes it is a "pushed" view, not child. I'll correct in main question. – JimmyJammed Mar 24 '13 at 04:47

2 Answers2

0

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.

rickster
  • 124,678
  • 26
  • 272
  • 326
0

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"];
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • 1
    Yeah I use notifications for a lot of apps, I was just hoping there was a way to use it in the delegate. I feel like delegates are much "cleaner" solutions but I guess I will have to use notifications. – JimmyJammed Mar 26 '13 at 20:50
  • Notifications and Delegates are not one and the same thing. Notifications are good for oneway communication, Delegation is good for two way communication. If you remember this key difference then you'll know why Apple use delegation pattern in most of the cases and notification pattern (keyboard notifications) . – Saqib Saud Mar 27 '13 at 06:58