Search code examples
iosdelegatesnsnotificationcenterpopoverchildviews

iOS - Call delegate method on main view from popover inner (pushed) view?


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;

    }
}

Solution

  • 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"];