在应用程序委托中添加的视图控制器未出现
View controller added in app delegate not appearing
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let auth: UIViewController =
storyBoard.instantiateViewController(withIdentifier: "Auth") as UIViewController
window?.rootViewController?.present(auth, animated: true, completion: nil)
return true
}
我收到错误...
Warning: Attempt to present
on whose view is not in the
window hierarchy!
我认为根控制器在应用程序生命周期的此时尚未正确配置。
我该怎么做?我想避免让根控制器必须检查它是否需要显示登录屏幕。
在此处进行更改,
let auth: AuthVC = storyBoard.instantiateViewController(withIdentifier: "Auth") as AuthVC
// AuthVC is your_VC_name
还有问题,可以问。
你可以这样做:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if Settings.appSettings.authToken != nil {
self.showMainController()
}
NotificationCenter.default.addObserver(forName: .authorizationOperationDidSuccess,
object: nil, queue: nil) { (notification) in
self.showMainController()
}
return true
}
private func showMainController() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller: UIViewController =
storyBoard.instantiateViewController(withIdentifier: "Main") as UIViewController
if self.window == nil {
self.window = UIWindow(frame: UIScreen.main.bounds)
}
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
}
private func showAuthorizationController() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller: UIViewController =
storyBoard.instantiateViewController(withIdentifier: "Auth") as UIViewController
if self.window == nil {
self.window = UIWindow(frame: UIScreen.main.bounds)
}
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
}
登录成功后
NotificationCenter.default.post(name: .authorizationOperationDidSuccess,
object: nil)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let auth: UIViewController =
storyBoard.instantiateViewController(withIdentifier: "Auth") as UIViewController
window?.rootViewController?.present(auth, animated: true, completion: nil)
return true
}
我收到错误...
Warning: Attempt to present on whose view is not in the window hierarchy!
我认为根控制器在应用程序生命周期的此时尚未正确配置。
我该怎么做?我想避免让根控制器必须检查它是否需要显示登录屏幕。
在此处进行更改,
let auth: AuthVC = storyBoard.instantiateViewController(withIdentifier: "Auth") as AuthVC
// AuthVC is your_VC_name
还有问题,可以问。
你可以这样做:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if Settings.appSettings.authToken != nil {
self.showMainController()
}
NotificationCenter.default.addObserver(forName: .authorizationOperationDidSuccess,
object: nil, queue: nil) { (notification) in
self.showMainController()
}
return true
}
private func showMainController() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller: UIViewController =
storyBoard.instantiateViewController(withIdentifier: "Main") as UIViewController
if self.window == nil {
self.window = UIWindow(frame: UIScreen.main.bounds)
}
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
}
private func showAuthorizationController() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller: UIViewController =
storyBoard.instantiateViewController(withIdentifier: "Auth") as UIViewController
if self.window == nil {
self.window = UIWindow(frame: UIScreen.main.bounds)
}
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
}
登录成功后
NotificationCenter.default.post(name: .authorizationOperationDidSuccess,
object: nil)