Tiago Fernandez
Tiago Fernandez

Reputation: 1473

How to get the view controller wrapped by a UIPopoverController from UIStoryboardPopoverSegue?

I have an UISplitViewController with the master having an UIViewController embedded in an UINavigationController. A toolbar button is responsible for bringing an UIPopoverController up, via segue. Such popover controller wraps an UIViewController also embedded in an UINavigationController, called SettingsViewController.

I can get a pointer to the UIPopoverController from the UIStoryboardPopoverSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender
{
  if ([segue.identifier isEqualToString:@"Settings"]) {
    UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue*) segue;

    SettingsViewController *settingsViewController = ... // TODO
    settingsViewController.popoverController = popoverSegue.popoverController;
  }
}

But I can't find a way to get a reference to the inner SettingsViewController. I don't want to use a static field accessible via class method, it would be a terrible workaround.

What am I missing to get it right?

Thanks for your help!

Upvotes: 0

Views: 2287

Answers (2)

Nick
Nick

Reputation: 3394

I have a very similar setup in my app as described at the beginning of the question.

In my prepareForSegue (and inside where I check the segue.identifier) I grab and set things on the popover's view controller like so:

settingsViewController *settingsPop = segue.destinationViewController;
settingsPop.someVar = self.var;

segue.destinationViewController makes it simple and easy.

Upvotes: 0

lukaswelte
lukaswelte

Reputation: 3001

UIPopoverSegue contains a UIPopoverController

UIPopoverController *popoverController = popoverSegue.popoverController;

With this you can easily get the contentViewController (view displayed in the popover)

UINavigationController *contentNC = (UINavigationController *) popoverController.contentViewController;

And from the content navigation controller, you get the actual view controller:

SettingsViewController *settingsVC = [contentNC.viewControllers lastObject];

Does this solve your problem?

Upvotes: 5

Related Questions