Search code examples
iosobjective-cuinavigationcontrollersegue

ObjectiveC: Add/Perform multiple segue in navigation controller


I'm implementing an application feature where it has 6-7 screens inflow. And user can left/close flow at any screen.

But when the user again applies for an application, they should jump to the last screen where he left and also he can able to go back to previous screens.

For e.g: I started to apply for application and completed till 4th screen and close. Again apply, I must jump directly to the 4th screen and also able to go back to 3rd->2nd->1st screen from the stack.

Current Code:

Segue identifires for screens from 1-7 in Storyboard are "screen1", "screen1" ... "screen7"

From HomeScreen.m

-(void)toPersonalApplication {

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Personal" bundle:nil];
    ScreenOne *screenOne = [storyboard instantiateViewControllerWithIdentifier:@"screenOne"];
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:screenOne];
    [self presentViewController:nav animated:YES completion:nil];
}

Checking if the user has already started an application process:

On ScreenOne.m

   - (IBAction)btnNextClick:(id)sender {

        if (doneProcessTill == 4) {

        // Should be execute something like this here
        // [self performSegueWithIdentifier:@"screen2" sender:self];
        // [self performSegueWithIdentifier:@"screen3" sender:self];
        // [self performSegueWithIdentifier:@"screen4" sender:self];
      }

    }

Appreciate your suggestion! Thanks


Solution

  • As stated in my comment a solution could look like this:

    - (void)toPersonalApplication {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Personal" bundle:nil];
    
        NSMutableArray *viewControllers = [NSMutableArray array];
        for (NSInteger i = 1; i <= doneProcessTill; ++i) {
            UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"screen%ld", (long)i]];
            [viewControllers addObject:viewController];
        }
    
        UINavigationController *navigationController = [UINavigationController new];
        navigationController.viewControllers = viewControllers;
    
        [self presentViewController:navigationController animated:YES completion:nil];
    }