Search code examples
iosnavigationback

Go back to unvisited page on IOS


I am unfamiliar with IOS development, so probably a simple question: is it possible that the user presses the "back" button on the top, and go to a (so far) non-visited page? Here is the picture: enter image description here

On window "1" user presses a button, the next window is "2". But here, if user presses the "back" button, go to page "3" (which was never opened so far)?

Thanks


Solution

  • You can simply redefine the back button by creating a new one in your second view controller like this :

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 60.0f, 30.0f)];
        [backButton setTitle:@"Back" forState:UIControlStateNormal];
        [backButton setTitleColor:self.view.tintColor forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(pushToNextController) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.navigationItem.leftBarButtonItem = backButtonItem;
    }
    
    - (void)pushToNextController {
        UIViewController *thirdViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"thirdViewController"];
        [self.navigationController pushViewController:thirdViewController animated:YES];
    }