简体   繁体   中英

Swift: Not able to dismiss modally presented LoginViewController

As SplitViewController loads, I am showing a Login Screen. On successful login, I need to go back to parent view controller. Somehow dismissal is not working for me. Here is the code:

ParentViewController:

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        if !appDelegate.loggedIn {
            self.performSegueWithIdentifier("loginScreen", sender: self)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }

Child ViewController:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.loggedIn = true
self.dismissViewControllerAnimated(true, completion: nil)

The dismissal part never works. It just hangs on Login Screen.

Try one of the following:

1) remove self. keep only dismissViewControllerAnimated(true, completion: nil)

or remove self. and make it:

2) presentingViewController.dismissViewControllerAnimated(true, completion: nil) or remove self. and try:

3) presentedViewController.dismissViewControllerAnimated(true, completion: nil)

Try this in your parent view controller:

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        if !appDelegate.loggedIn {
            let loginVC: UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier("LoginViewController") as UIViewController
            loginVC = UIModalTransitionStyle.CoverVertical
            self.parentViewController?.presentViewController(loginVC, animated: true, completion: nil)
        }
}

You're instantiating the new view controller by its own name rather than by the segue name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM