YoannLth
YoannLth

Reputation: 197

Parent view controller not dismissed

I have an issue with the presentViewControllerfunction.

I have a login menu (A) and a button who display a login form (B). On success login, I present a third view with self.presentViewController (C).

Login menu (A) -> Login form (B) -> Content (C)

It's work, I can present the third view (C) but the login menu (A) is always visible (I can see it in UI Debug mod).

So, how to close the A ViewController?

Upvotes: 0

Views: 564

Answers (3)

Wolverine
Wolverine

Reputation: 4329

If we consider the presentVieController mechanism, this is not a problem or bug. Still if you feel that you don not want your previous controller's remain in Hierarchy, Then you have to change the flow.

Currently your flow is :

A -> RootViewController. B -> LoginScreen C -> Container

I would suggest to refer below answer for best practise.

Best practices for Storyboard login screen, handling clearing of data upon logout

if you give thoughts on handling the flow, you can solve your problem. here are my suggestions

Suggestions :

  1. You set C as a rootController, In C's ViewDidLoad , you check weather User is logged in or not, If user is not logged in, you can present A , And From there you can present B. Once you successfully get logged in, you can dismiss A and B And refresh your rootController C as per logged user values.

To dismiss multiple view controller, you can try below code

-(void)dismissModalStack {
    UIViewController *vc = self.presentingViewController;
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:YES completion:NULL];
}
  1. You can also use the ContainerView mechanism, You set C as rootController, And in C , you take an ContainerView In which you can show the A And B and after login, you can remove them and refresh C.

Hope it Helps!

Upvotes: 1

Carien van Zyl
Carien van Zyl

Reputation: 2883

presentViewController, presents a view controller modally. In other words, over the current view controller’s content, and the current view controller will not get deallocated.

Use the following code for your sample:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: <Identifier as in Storyboard of C>)
(UIApplication.shared.delegate as! AppDelegate).window?.rootViewController = vc

If you would like to include a transition, you could replace the last line with something as the following:

UIView.transition(from: currentRootViewController!.view, to: vc.view, duration: 0.8, options: .transitionCrossDissolve, completion: { (_) in
      (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController = vc
})

Upvotes: 1

Waqar Khalid
Waqar Khalid

Reputation: 31

I think this tutorial has your answer https://www.youtube.com/watch?v=WIaRs2d6Xm0

Upvotes: 0

Related Questions