Replace segue creating new instance

半腔热情 提交于 2019-12-11 07:28:43

问题


I've run into a problem with a SplitViewController app. When I select a row from the table in the master view I'm replacing the detail view with a replace segue. The problem is each time this happens it creates a new instance/reloads the detail view controller. Is there any way I can have it so it will replace the detail view with the previous instance if it has already been instantiated. I'd like the state for the detail views to be remembered, such as input into textfields.


回答1:


Cannot be done. A segue will always instantiate a view controller




回答2:


I use the replace segue, but in the MasterVC I implement

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {

    if ([identifier isEqualToString:@"showCalculatorViewController"]) {

        if (self.splitViewController) {

            NSObject *currentDetail = [self.splitViewController.viewControllers lastObject];

            if ([currentDetail isKindOfClass:[RootCalcViewController class]]) {

....

Then I fill in the new properties of the existing VC, and return NO.

But I do not have a UINAvigationController as my initial Detail View Controller, since I handle the navigation from the masterVCs.

Also, in AppDelegate, didFinishLaunchingWithOptions the correct detail VC needs to be set up. The generated code assumes that it is a UINavigationController. Similarly in the generated MasterViewController, viewDidLoad. This will save you some app crashes if you replace the detail UINAvigationController.

When I load different view controllers, I let the "Replace segues" replace them.




回答3:


Or - you could just make your DetailController implement Singleton patter and forward any calls to +new, +alloc or the like to your singleton method.



来源:https://stackoverflow.com/questions/9994876/replace-segue-creating-new-instance

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