在应用启动时呈现不同的 UIViewController
Presenting different UIViewController on the app Launch
我需要在我的应用启动时呈现不同的 UIViewController
。我有一个 "login" 页面供用户交互。一旦用户登录或创建一个帐户,它就会进入另一个 UIViewController
与地图进行交互。我在网上看过,到目前为止我知道我们应该在 AppDelegate.swift
文件中进行。我还没有完成用户是否登录的声明,因为我仍然运行 遇到一些错误
AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 2.0)
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = MainNavigationContoller()
return true
}
我还有另一个 swift 文件,其中 MainNavigationContoller 应该调用 mainviewController
override func viewDidLoad() {
super.viewDidLoad()
let isloggedIn = false
if isloggedIn == false {
self.present(mainViewController(), animated: true, completion: nil)
} else {
self.present(mapViewController(), animated: true, completion: nil)
}
}
应用程序使用 launchScreen 启动,但随后向 mainViewController 发送错误,例如 线程 1:致命错误:在隐式展开可选值时意外发现 nil
您没有正确实例化视图控制器。
这是我用来制作根视图控制器的函数,将其放入您的 AppDelegate 中。
func makeRootVC(storyBoardName : String, vcName : String) {
let vc = UIStoryboard(name: storyBoardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
let nav = UINavigationController(rootViewController: vc)
nav.navigationBar.isHidden = true
self.window?.rootViewController = nav
let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.6
UIView.transition(with: self.window!, duration: duration, options: options, animations: {}, completion: nil)
}
现在在您的 Appdelegate 中,将您的代码替换为:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 2.0)
self.makeRootVC(storyboardName: "Main", vcName : "YourVCStoryboardId")
return true
}
并在您的 MainNavigationController 中,
override func viewDidLoad() {
super.viewDidLoad()
let isloggedIn = false
let appDelegateObj = UIApplication.shared.delegate as! AppDelegate
if isloggedIn == false {
appDelegateObj.makeRootVC(storyboardName: "Main", vcName: "mainViewController")
} else {
appDelegateObj.makeRootVC(storyboardName: "Main", vcName: "mapViewController")
}
}
注意:打开故事板并为每个控制器提供一个 StoryboardID。我更喜欢将它们命名为与 ViewController 相同的名称,因为它很容易记住。在 vcName 中,我们需要传递我们要呈现的控制器的故事板 ID。
更新:
以上代码用于制作根视图控制器,如果您想推送控制器,可以改用此代码:
extension UIViewController {
func pushVC(storyboardName : String, vcname : String) {
let vc = UIStoryboard.init(name: storyboardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcname)
vc.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(vc, animated: true)
}
}
在您的 MainNavigationController 中,如果您不想在 viewDidLoad 中创建根视图控制器,而只想推送控制器,则可以像这样使用上面的代码:
override func viewDidLoad() {
super.viewDidLoad()
let isloggedIn = false
if isloggedIn == false {
self.pushVC(storyboardName: "Main", vcName: "mainViewController")
} else {
self.pushVC(storyboardName: "Main", vcName: "mapViewController")
}
}
我需要在我的应用启动时呈现不同的 UIViewController
。我有一个 "login" 页面供用户交互。一旦用户登录或创建一个帐户,它就会进入另一个 UIViewController
与地图进行交互。我在网上看过,到目前为止我知道我们应该在 AppDelegate.swift
文件中进行。我还没有完成用户是否登录的声明,因为我仍然运行 遇到一些错误
AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 2.0)
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = MainNavigationContoller()
return true
}
我还有另一个 swift 文件,其中 MainNavigationContoller 应该调用 mainviewController
override func viewDidLoad() {
super.viewDidLoad()
let isloggedIn = false
if isloggedIn == false {
self.present(mainViewController(), animated: true, completion: nil)
} else {
self.present(mapViewController(), animated: true, completion: nil)
}
}
应用程序使用 launchScreen 启动,但随后向 mainViewController 发送错误,例如 线程 1:致命错误:在隐式展开可选值时意外发现 nil
您没有正确实例化视图控制器。
这是我用来制作根视图控制器的函数,将其放入您的 AppDelegate 中。
func makeRootVC(storyBoardName : String, vcName : String) {
let vc = UIStoryboard(name: storyBoardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
let nav = UINavigationController(rootViewController: vc)
nav.navigationBar.isHidden = true
self.window?.rootViewController = nav
let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.6
UIView.transition(with: self.window!, duration: duration, options: options, animations: {}, completion: nil)
}
现在在您的 Appdelegate 中,将您的代码替换为:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 2.0)
self.makeRootVC(storyboardName: "Main", vcName : "YourVCStoryboardId")
return true
}
并在您的 MainNavigationController 中,
override func viewDidLoad() {
super.viewDidLoad()
let isloggedIn = false
let appDelegateObj = UIApplication.shared.delegate as! AppDelegate
if isloggedIn == false {
appDelegateObj.makeRootVC(storyboardName: "Main", vcName: "mainViewController")
} else {
appDelegateObj.makeRootVC(storyboardName: "Main", vcName: "mapViewController")
}
}
注意:打开故事板并为每个控制器提供一个 StoryboardID。我更喜欢将它们命名为与 ViewController 相同的名称,因为它很容易记住。在 vcName 中,我们需要传递我们要呈现的控制器的故事板 ID。
更新:
以上代码用于制作根视图控制器,如果您想推送控制器,可以改用此代码:
extension UIViewController {
func pushVC(storyboardName : String, vcname : String) {
let vc = UIStoryboard.init(name: storyboardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcname)
vc.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(vc, animated: true)
}
}
在您的 MainNavigationController 中,如果您不想在 viewDidLoad 中创建根视图控制器,而只想推送控制器,则可以像这样使用上面的代码:
override func viewDidLoad() {
super.viewDidLoad()
let isloggedIn = false
if isloggedIn == false {
self.pushVC(storyboardName: "Main", vcName: "mainViewController")
} else {
self.pushVC(storyboardName: "Main", vcName: "mapViewController")
}
}