Proper way to call popToViewController - iOS

╄→尐↘猪︶ㄣ 提交于 2019-12-12 02:28:54

问题


I am having an argument with my team member regarding popToViewController.

For me best approach is Can i pop to Specific ViewController?

But he think we should create a static instance of the viewController and then call

-(void)takeToPreviousViewController {
    if([IVPreviousViewController sharedInstance]) {
        [self.navigationController popToViewController:[IVPreviousViewController sharedInstance] animated:YES];
    }
}

I strongly appose this approach because we're creating a public method in IVPreviousViewController which has no relation to the currentViewController. We should always avoid method expose like this.

Can anyone point me a solid reason about the best approach.

Update:

IVPreviousViewController.h

__weak static IVPreviousViewController * staticEventDetailViewController;

    +(IVPreviousViewController *)sharedInstance;

IVPreviousViewController.m

+(IVPreviousViewController *)sharedInstance {
    return staticEventDetailViewController;
}

Reason of this approach - On a particular use case in our viewController it has to popbackto IVPreviousViewController


回答1:


This is my take on this:

I support your opinion. We should avoid static shared instances where ever possible. Static instances remains in memory until app life time. All the other objects being referenced from this instance also then remain in memory.

With the approach mentioned in above shared link VC that is no more needed will be taken out of memory. Also, if you do not have many VCs, better have a weak reference to target VC from source VC instead of looping and selecting the target VC.

And yes, in context of objective C for better readability, header file should contain only those APIs that are really needed by outside world.




回答2:


I think you can do something like this

-(void)popToVC:(ClassNameOfTheViewController)controllerClass{
 for(UIViewController *vc in self.navigationController.viewControllers)
    if( [vc isKindOfClass:[controllerClass class]]){
     [self.navigationController popToViewController:vc animated:YES];
   }
}


来源:https://stackoverflow.com/questions/32546217/proper-way-to-call-poptoviewcontroller-ios

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