After my app is finished launching, I want my app to open a LoginViewController
if a current user is not logged in. Otherwise, it goes to a ViewController
which is at the index of 0 (default value). So if not logged in, then show a modal view over UITabBarController
.
My initial viewcontroller is a UITabBarController
, which is created in UIStoryboard
. It is check-marked as Initial View
and is connected to other five viewcontrollers graphically.
Actually, I think I know the reason why a LoginViewController
does not appear. It is because the value result was nil (I checked in the console). But why?? Other than this behavior, everything is working well. UITabBarController
seems to be working without any problem.
My LoginViewController
is embedded with UINavigationViewController
. This code is inside a ViewController
which is at a selected index of 0. It is put inside viewDidAppear()
method
let main = UIStoryboard(name: "Main", bundle: nil)
let view = main.instantiateViewController(withIdentifier: "login") as! LoginViewController
let nav = UINavigationController(rootViewController: view)
self.tabBarController?.present(nav, animated: false, completion: nil)
However, it is not working.
窗口RootViewController的更改的UINavigationController如果用户没有登录,如果他是将其更改为tabBarController
You should implement the logic of deciding what is the desired initial view controller to be the root (based on whether the user logged in or not) before navigating to any view controller, application(_:didFinishLaunchingWithOptions:) method would be appropriate for such an implementation:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// let's assume that you are recognizing if the user logged in by a flag called 'isLoggedin':
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if isLoggedin { // show display main view controller
let mainViewController = storyboard.instantiateViewController(withIdentifier: "mainViewController")
// setup any needed config for mainViewController...
self.window?.rootViewController = mainViewController
} else { // display login view controller
let loginViewController = storyboard.instantiateViewController(withIdentifier: "loginViewController")
// setup any needed config for loginViewController...
self.window?.rootViewController = loginViewController
}
self.window?.makeKeyAndVisible()
return true
}
Or as a shorter version:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let isLoggedin = false
let initialViewController = storyboard.instantiateViewController(withIdentifier: isLoggedin ? "mainViewController" : "loginViewController")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
At this point, after the app finished launching (without jumping to any view controller yet), the initial view controller would be displayed based on the user logging in validation. I would assume that it would be a better behavior unless there is a specific requirement to present the login view controller on the of the tabbar controller; Logically, there is no even need to navigate to the tabbar controller if the user didn't login yet.
I run your code, Here is issue:
2018-01-18 16:23:52.845273+0800 try[8180:315334] Warning: Attempt to present on whose view is not in the window hierarchy!
self.tabBarController?.present(nav, animated: false, completion: nil)
this line is very wired. I seldom saw.
So you can also let a child controller of self.tabBarController
to present nav
.
The choose one of the tab bar.
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.