【问题标题】:How to present a view controller before main one is loaded?如何在加载主视图控制器之前呈现视图控制器?
【发布时间】:2021-01-07 15:45:05
【问题描述】:

我有一个视图控制器,但在viewDidLoad() 方法中我调用了另一个名为authenticateUserAndConfigureView() 的方法,当当前用户为零时,它会显示另一个视图控制器。但是,它会稍微延迟一些 - 它首先加载主视图控制器,然后从 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)

        
        
    }

}

【问题讨论】:

  • 我不使用登录屏幕,但我可以说过去几年这里有 几个 问题 - 以及(我假设)许多教程。很好地展示了你的尝试。你做过搜索吗? SO 上的“[swift] 登录视图”快速返回 2575 次点击。
  • 是的,我尝试过搜索。也许我是一个糟糕的搜索者。然而,我设法通过在self.present(navController, animated: true, completion: nil) 中将“动画”更改为 false 来解决这个问题。但是我在控制台中收到两个警告 - 不鼓励从分离的视图控制器呈现视图控制器,以及对
  • 可能,至少有一点。警告确实意味着什么——即使事情有效。我已经收到了这两个警告,并且至少了解他们试图告诉您的内容。按顺序... (1) 一个独立的 VC 就是这个意思。从事物的声音来看,您的根 VC(在 AppDelegate 或更多 SceneDelegate 中声明)可能正在呈现您的登录 VC。但是,它不在视图层次结构中。 (2) 根据我的经验,不平衡调用意味着您正在尝试使用导航控制器,而 VC 堆栈不一定正确。对我来说,#2 是更重要的问题。

标签: ios swift xcode viewcontroller


【解决方案1】:

你可以在viewWillAppear里面插入这一行

override func viewWillAppear(_ animated:Bool) {
   super.viewWillAppear(animated)
   authenticateUserAndConfigureView()
}

在呈现 MainVC 之前进行此检查,并根据它呈现合适的 vc

【讨论】:

  • 我尝试插入这一行,但现在显示错误Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0x7f877c04bc00>.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多