简体   繁体   中英

How to go back to home page or previous viewcontroller after login in swift 3

I am developing an app with a navigation controller connected to a tabbar controller and some other viewcontroller attached to tabbar controller as shown in the image:

在此处输入图片说明

Now I want to show user the login page only when they click on the second tab and not logged in. So in the second tab controller, I have this:

if let token = cookies["token"] {
        //do something
} else {
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "loginView")
    self.tabBarController?.setViewControllers([vc!], animated: true)
}

And in my LoginViewController, I would like to go back to the previous page after successfully logged in. I did some research about the topic. Here's what I've tried. I tried this:

let vc = self.storyboard?.instantiateViewController(withIdentifier: "minePage") as! MineViewController
self.tabBarController?.setViewControllers([vc], animated: true)

It brings up the page, but only shows the tabbar not the navigation bar anymore. I also tried this:

self.navigationController?.pushViewController(vc, animated: true)

It also brings user back to the "minePage", but only shows navigation bar not the tabbar. I also tried to add a unwind segue in the storyboard and a function in first view controller:

@IBAction func prepareForUnwindWithSegue(segue: UIStoryboardSegue){
}

and tried this:

self.dismiss(animated: true, completion: nil)

and nothing happens after logged in. My question is

  1. Is the storyboard setting a good practice to add login view? Some stackoverflow answers suggest put the login view before everything. But only part of the app requires login. So it is not suitable.
  2. How to go back to previous page or a default homepage (In my case the first tabbar controller view)? Both cases are needed in the app.

Thanks.

Hi could you not just put the "full" logged in view controller following your login page. So put a segue from your login view controller (not the login button) to the new view controller to the right (new one not in diagram above) and then conditionally call it if the login attempt is valid?

That way you have a clear division of what is seen before login and after login?

Then you would just add logic to the first viewController in that tab to check if user is logged in, and if they are, bypass the all the way to the new logged in view controller through a similar segue (from the first VC to the new one that conditionally is fired).

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