Tom Lilletveit
Tom Lilletveit

Reputation: 2002

Making a popover segue´s View Controller stay persistent (only allocate one instance)

I programmed my app initially for iPhone using a tab bar controller were the view controllers are initialized once and stays persistent - it does not initialize a new instance of the view controller when I tap the tab bar.

on the iPad I am using a different GUI were instead I have one main view that always stays on the screen, and the rest are popovers segueing from the main view.

I want the popovers to stay persistent (only initialize once) what is the best way of archiving this. If I had been using *.xib files I could have initialized the popover´s view controllers in the main view and then sent a copy of them when segueing, and that way only ever have one instance of them. But I am using Storyboards.

Upvotes: 0

Views: 383

Answers (2)

Tom Lilletveit
Tom Lilletveit

Reputation: 2002

I found a solution and actually it´s easy, just use a UIPopoverController and initialize it with the view controller you want to present. In this way it will not instantiate a new instance each time a popover is requested.

if (!popoverController)
    popoverController = [[UIPopoverController alloc]initWithContentViewController:bellViewController];


[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
popoverController.delegate=self;

Upvotes: 0

rdelmar
rdelmar

Reputation: 104092

You can't use segues if you want your controllers to be persistent, because segues always instantiate new controllers. You can still use the storyboard, but you have to leave the controllers unconnected, and instantiate them in code, and assign them to a strong property. So, something like:

-(void)presentPopover {
    if (! self.vc) {
        self.vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyController"];
    }
    // do what you want here to put vc on screen
}

Upvotes: 1

Related Questions