0

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

Community
  • 1
  • 1
Tariq
  • 9,861
  • 12
  • 62
  • 103

2 Answers2

2

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.

Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • hey @Abhinav I have added some more update in my question. Let me know if you have any more insight. VC will never remain in the memory because he has created __weak static. But i still think its not a best approach – Tariq Sep 13 '15 at 04:34
  • As I said, always avoid singleton especially in this case we are loosing more than the gain. The functionality can be achieved by holding a weak reference to `IVPreviousViewController`. No need to keep `IVPreviousViewController` always around in memory. Run the app on instruments and this will make it more clear. – Abhinav Sep 13 '15 at 04:38
  • Hey @Abhinav, Sorry to bring you into our quarrel :) We are using static weak pointer to reference the IVPreviousViewController. So any suggestions/ideas on why not to use a weak pointer even? – Shashank Sep 13 '15 at 05:48
  • To me, code is not conveying the right intention; basically I find it ambiguous. Here `sharedInstance` conveys the idea of a singleton object that is shared between multiple objects. However, in this context, it seems be an object with weak reference property i.e. with each instantiation of `IVPreviousViewController `, value of `sharedInstance` will vary which design wise is not correct. – Abhinav Sep 13 '15 at 09:09
  • 1
    In your code you may be certain that instantiation would be happening in one particular flow but tomorrow if you leave the project, it would be little tricky for new person to understand the reason for adding a `sharedInstance` which is actually not a shared instance. Hope this makes sense! – Abhinav Sep 13 '15 at 09:09
1

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];
   }
}
Saheb Roy
  • 5,899
  • 3
  • 23
  • 35