Storyboards - how to perform segues automatically without animation and showing the screen

StackOverflow https://stackoverflow.com/questions/23524448

  •  17-07-2023
  •  | 
  •  

I have an application where the user has to enter some information on the initial screens, such as the login credentials. All the navigations are done using a storyboard and segues, which automatically sets the navigation bar on top.

I have two storyboards that share the same controllers, so I use the same name for the segues (ipad and iphone versions).

So when the user comes back to the application, I read the core data and know that he has already performed the initial steps, so I would like to "skip" those screens.

Problem: I can only execute the segues after the view is visible, otherwise the navigation is screwed up. But when doing so, the user sees the screen briefly and sees the animation "pushing" that screen away. I'd like to keep the navigation history on the navigation bar, that is why I want to use the segues and all the logic associated with them.

All the solutions point to creating the views programatically and putting them on the stack, but I'd like to take advantage of the storyboards.

有帮助吗?

解决方案

Here is one way of doing it;

In your StoryBoard, assign an identifier to the second view controller (this is done on the identity inspector, setting the Storyboard ID field). In the code below, i have named mine secondVC;

Then in the viewDidLoad for your first controller (the one you want to skip but come back to) do something like this;

- (void)viewDidLoad{
   [super viewDidLoad];
   /// validate  viewController one being displayed
   if(dontDisplayFirstController){

      UIStoryboard *storyBoard = self.storyboard;
      UIViewController *targetViewController = [storyBoard instantiateViewControllerWithIdentifier:@"secondVC"];
      UINavigationController *navController = self.navigationController;


      if (navController) {
        [navController pushViewController:targetViewController animated:NO];
      }
   }
}

This will efectivly push to the second viewController whilst still maintaining viewController one in the navigation tree.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top