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

与世无争的帅哥 提交于 2019-12-11 19:25:18

问题


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.


回答1:


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
}



回答2:


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;


来源:https://stackoverflow.com/questions/18806482/making-a-popover-segue%c2%b4s-view-controller-stay-persistent-only-allocate-one-inst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!