如何在加载主视图控制器之前呈现视图控制器?
How to present a view controller before main one is loaded?
我有一个视图控制器,但在 viewDidLoad()
方法中,我调用了另一个名为 authenticateUserAndConfigureView()
的方法,其中当当前用户为 nil 时,它会显示另一个视图控制器。然而,它出现时有一点延迟——它首先加载主视图控制器,然后从 authenticateUserAndConfigureView()
方法加载第二个视图控制器。
这是为什么?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
authenticateUserAndConfigureView()
setUpElements()
}
func setUpElements() {
// Style the elements
Utilities.styleFilledButton(signUpButton)
Utilities.styleHollowButton(loginButton)
}
func authenticateUserAndConfigureView() {
if Auth.auth().currentUser == nil {
print("No user signed in..")
} else {
print("User is already signed in.")
let navController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeNVC")
navController.modalPresentationStyle = .fullScreen
self.present(navController, animated: true, completion: nil)
}
}
你可以在里面插入这一行 viewWillAppear
override func viewWillAppear(_ animated:Bool) {
super.viewWillAppear(animated)
authenticateUserAndConfigureView()
}
OR 在呈现 MainVC 之前进行此检查并根据它呈现合适的 vc
我有一个视图控制器,但在 viewDidLoad()
方法中,我调用了另一个名为 authenticateUserAndConfigureView()
的方法,其中当当前用户为 nil 时,它会显示另一个视图控制器。然而,它出现时有一点延迟——它首先加载主视图控制器,然后从 authenticateUserAndConfigureView()
方法加载第二个视图控制器。
这是为什么?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
authenticateUserAndConfigureView()
setUpElements()
}
func setUpElements() {
// Style the elements
Utilities.styleFilledButton(signUpButton)
Utilities.styleHollowButton(loginButton)
}
func authenticateUserAndConfigureView() {
if Auth.auth().currentUser == nil {
print("No user signed in..")
} else {
print("User is already signed in.")
let navController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeNVC")
navController.modalPresentationStyle = .fullScreen
self.present(navController, animated: true, completion: nil)
}
}
你可以在里面插入这一行 viewWillAppear
override func viewWillAppear(_ animated:Bool) {
super.viewWillAppear(animated)
authenticateUserAndConfigureView()
}
OR 在呈现 MainVC 之前进行此检查并根据它呈现合适的 vc