Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modal Segue Chain

I have an iOS app that has a log in view (LognnViewController) and once a user is successfully authenticated they are taken to another view (DetailEntryViewController) to enter some simple details.
Once the details are entered the user is taken to the main part of the app that consists of a tab controller (TabViewController) that holds a variety of other views. The LogInViewController performs a modal segue to the DetailEntryViewController and the DetailEntryViewController then performs a modal segue to the TabViewController so I have kind of a modal segue chain going to get into the app. When a user logs out I want to go all the way back to the LogInViewController but when I do a:

[self.presentingViewController dismissModalViewControllerAnimated:YES];

...it pops the TabViewController and I end up back at the DetailEntryViewController instead of the first LogInViewController. Is there any way I can pop back to the first view controller easily or does doing this modal segue chain thing prevent me from that. I got the bright idea to put some code in the DetailEntryViewController viewWillAppear: that would automagically pop itself if the user had logged out but apparent making calls to dismiss a modal controller are not allowed in viewWillAppear: viewDidLoad:, etc.

Any ideas on how to make this happen?

like image 408
Craig Koster Avatar asked Dec 05 '12 17:12

Craig Koster


People also ask

What is a modal segue?

A modal Segue is just one VC presenting another VC modally. The VCs don't have to be part of a navigation controller and the VC being presented modally is generally considered to be a "child" of the presenting (parent) VC. The modally presented VC is usually sans any navigation bars or tab bars.

How to call unwind segue programmatically?

To trigger an unwind segue programmatically (or any other segue), we need to give it an identifier in the storyboard. There is no arrow in a storyboard for unwind segues. To select one, you have to find it in the document outline on the left side of Interface Builder.

What is present Modally Swift?

The Different Types of Segues Present Modally – Presents the ViewController modally and directly, with various animations that are set by the presentation option. On iPhone it covers the whole screen, but on iPad it commonly presents as a box in the center of the screen.


1 Answers

I think this is not the best structure to implement your app. Modal controllers are supposed to be for temporary interruptions to the flow of the program, so using a modal to get to your main content is not ideal. The way I would do this is to make your tab bar controller the root view controller of the window, and then in the first tab's controller, present the login controller modally from the viewDidAppear method, so it will appear right away (you will briefly see the first tab's view unless you uncheck the "animates" box in the segue's attributes inspector). Present the details controller from that one, and then dismiss both modal controllers to get back to your main content. When the user logs out, just present that login controller again. I implement this idea like this. In the first tab's view controller:

- (void)viewDidLoad {
    [super viewDidLoad];
    _appStarting = YES;
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (_appStarting) {
       [self performSegueWithIdentifier:@"Login" sender:self];
        _appStarting = NO;
    }
}

Then in the last (second in your case) modal view controller, I have a button method:

-(IBAction)goBackToMain:(id)sender {
    [self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
like image 196
rdelmar Avatar answered Sep 30 '22 17:09

rdelmar