Search code examples
iphoneiosobjective-cparentparentviewcontroller

Objective check which view the user has just come from


I have an Application where you can go to one view from two controllers and I was wondering whether it was possible to check which it came from so that I can do different things depending on the controller it came from.

Thanks in advance


Solution

  • You can access the UINavigation stack to see which view is before the previous one assuming you push the new view.

    Class aClass = [[[self.navigationController viewControllers] objectAtIndex:self.navigationController.viewControllers.count - 2] class];
    
    if (aClass == [UIViewControllerA class])
        //Do something
    else if (aClass == [UIVIewControllerB class])
        //Do something else
    

    Or create a custom init method for the single view you push to that allows you to pass in a variable as to which view it came from (sorry, really wordy).

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil isFromViewA:(bool)isFromViewA