sapatos
sapatos

Reputation: 1304

Storyboard navigation - pushing and removing from stack

I have two storyboards in the same ios application.

Storyboard 1 is the login.storyboard. Storybaord 2 is the processing.storyboard.

login.storyboard has the following scenes:

1) welcome

2) login

processing.storyboard has

1) start

2) images

3) description

4) finish

login.storyboard handles login whilst processing.storyboard creates and object for uploading.

My understanding of the stack is as follows:

Navigate from welcome to login gives:

1:[welcome]-[login.storyboard]

2:[login]-[login.storyboard]

after login I push the processing.storyboard using

- (void) pushStory: (NSString *) story {

UIStoryboard *settingsStoryboard = [UIStoryboard storyboardWithName:story bundle:nil];
UIViewController *initialSettingsVC = [settingsStoryboard instantiateInitialViewController];
initialSettingsVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:initialSettingsVC animated:YES completion:nil];
}

the stack should now be:

1:[welcome]
2:[login]
3:[start]

I might get to description in the workflow before I decide to click logout (available on each page), my stack at this point would be

1:[welcome]-[login.storyboard]

2:[login]-[login.storyboard]

3:[start]-[processing.storyboard]

4:[images]-[processing.storyboard]

5:[description]-[processing.storyboard]

logout should take me back to [welcome], my question is with the storyboards how would I clear the stack back to [welcome] and ensure the login.storyboard is current.

There's a gap in my knowledge here, as I've just gotten back into iphone dev after 6 years or so and not seen these before.

I had thought of just pushing login.storyboard onto the stack but that would just make the stack continue to grow instead of clearing it out

Upvotes: 1

Views: 395

Answers (2)

Imanou Petit
Imanou Petit

Reputation: 92489

Use unwind segues.

Add this method in [welcome]:

-(IBAction)reset:(UIStoryboardSegue *)segue
{
    NSLog(@"Back to Welcome");
}

In Interface Builder, create UIButtons in [start], [images] and [description] then link each of these buttons to the green "Exit" button of their respective viewControllers and select reset: in the pop-up menu that appears.

(See WWDC 2012 session video "Adopting Storyboards in Your App" for more details about unwind segues [starts at 38 minutes].)

Upvotes: 1

user3386109
user3386109

Reputation: 34839

You can pop back to any arbitrary point in the stack, for example

    [self.navigationController popToRootViewControllerAnimated:YES];         // all the way back to the first view controller
    [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];  // back to the second view controller 
    [self.navigationController popViewControllerAnimated:YES];               // back to the previous view controller

This answer assumes that storyboard 1 has

  • navigation controller
  • welcome view controller
  • login view controller

and storyboard 2 has

  • navigation controller
  • start view controller
  • images view controller
  • description view controller
  • finish view controller

Note that the navigation controller in storyboard 2 is never actually instantiated but is needed so that the other view controllers in storyboard 2 can be connected by segues. When navigating from the login view controller to the start view controller the code should look similar to this

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ControllerC"];
[self.navigationController pushViewController:vc animated:YES];

Note that this doesn't instantiate the initialViewController since that's the navigation controller, and we don't want another navigation controller. Instead, give the start view controller a Storyboard ID under the Identity inspector, and then instantiate the start view controller directly. After instantiating the start view controller, push it onto the existing navigation controller. You may want to hide the back button if you don't want the user to navigate back to the login view controller.

Upvotes: 1

Related Questions