【问题标题】:Error setting root view controller with SWReveal in swift在 swift 中使用 SWReveal 设置根视图控制器时出错
【发布时间】:2020-02-15 01:26:21
【问题描述】:

我[正在尝试根据条件在应用程序委托中设置根视图控制器。当应用程序启动并将根视图控制器设置为具有代码以显示后视图控制器的控制器时,此错误:“致命错误:在展开可选值时意外发现 nil”

应用代理

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    let db = Firestore.firestore()
    var docRef: DocumentReference!
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let authListener = Auth.auth().addStateDidChangeListener { (auth, user) in
        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        if user != nil{
            var userId = user?.email
            docRef = Firestore.firestore().document("user/\(userId!)")
            docRef.getDocument(completion: { (docSnapshot, error) in
                guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}
                let data = docSnapshot.data()
                variables.userType = data!["tipo"] as? String ?? ""
            })

            if variables.userType == "doctor" {
                let consulta = storyboard.instantiateViewController(withIdentifier: "ConsultaController")
                self.window?.rootViewController = consulta
                self.window?.makeKeyAndVisible()
            } else if variables.userType == "paciente"{

            }
        } else {
            let consulta = storyboard.instantiateViewController(withIdentifier: "ConsultaController")
            self.window?.rootViewController = consulta
            self.window?.makeKeyAndVisible()
        }
    }
    return true
}

视图控制器中触发错误的代码。

编辑:加载初始视图控制器时触发错误。初始视图控制器由应用委托中的条件确定

override func viewDidLoad() {
    super.viewDidLoad()
    setUpSWReveal()
}

func setUpSWReveal(){
    menuBtn.target = self.revealViewController()
    menuBtn.action = #selector(SWRevealViewController.revealToggle(_:))
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) //error triggered here
    self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
}

【问题讨论】:

  • 您能否更具体地说明错误是在哪里触发的?最佳实践是在代码中添加断点并逐行执行,检查变量以确定它们何时不是您所期望的或观察崩溃。 return true 也将在变量 var 填充之前很久执行。
  • 加载初始视图控制器时触发错误。
  • 好的。该代码存在一些问题。最重要的是,这段代码if variables.userType == "doctor" { 将执行 before variables.userType 在 Firestore 闭包中分配。代码比 Internet 更快,从 Firestore 返回的数据仅在 Firestore 调用后的闭包内有效。在这里查看我的答案Firestore is asynchronous
  • 哦。你为什么从addStateDidChangeListener返回true?它不使用布尔返回。
  • 它从 didFinishLaunchingWithOptions 返回 true

标签: ios swift firebase swrevealviewcontroller


【解决方案1】:

我通过在每种情况下设置前后视图控制器来修复它。我使用的是导航控制器,所以下面代码的某些部分是导航控制器的属性

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    FirebaseApp.configure()
    let db = Firestore.firestore()
    var docRef: DocumentReference!
    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

    let authListener = Auth.auth().addStateDidChangeListener { (auth, user) in
        if user != nil{
            var userId = user?.email
            docRef = Firestore.firestore().document("user/\(userId!)")
            docRef.getDocument(completion: { (docSnapshot, error) in
                guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}

                let data = docSnapshot.data()
                variables.userType = data!["tipo"] as? String ?? ""
                if variables.userType == "paciente" {
                    let frontNavigationController:UINavigationController
                    let revealController = SWRevealViewController()
                    var mainRevealController = SWRevealViewController()

                    let front = storyboard.instantiateViewController(withIdentifier:  "MedicionViewContoller") as! MedicionVC
                    let rear = storyboard.instantiateViewController(withIdentifier: "MenuController") as! MenuVC

                    frontNavigationController =  UINavigationController.init(rootViewController: front)
                    frontNavigationController.navigationBar.barTintColor = UIColor.init(red: 12.0/255.0, green: 73.0/255.0, blue: 120.0/255.0 , alpha: 1.0)
                    frontNavigationController.navigationBar.titleTextAttributes = [
                        NSAttributedString.Key.foregroundColor : UIColor.white,
                        NSAttributedString.Key.font : UIFont(name: "Avenir-Heavy", size: 45)!
                    ]
                    frontNavigationController.navigationItem.leftBarButtonItem?.action = #selector(SWRevealViewController.revealToggle(_:))
                    revealController.frontViewController = frontNavigationController
                    revealController.rearViewController = rear
                    revealController.delegate = self
                    mainRevealController  = revealController

                    UIApplication.shared.delegate!.window??.rootViewController = mainRevealController
                    //
                }
                else {
                    let frontNavigationController:UINavigationController
                    let revealController = SWRevealViewController()
                    var mainRevealController = SWRevealViewController()

                    let front = storyboard.instantiateViewController(withIdentifier:  "ConsultaController") as! ConsultaVC
                    let rear = storyboard.instantiateViewController(withIdentifier: "MenuController") as! MenuVC

                    frontNavigationController =  UINavigationController.init(rootViewController: front)
                    frontNavigationController.navigationBar.barTintColor = UIColor.init(red: 12.0/255.0, green: 73.0/255.0, blue: 120.0/255.0 , alpha: 1.0)
                    frontNavigationController.navigationBar.titleTextAttributes = [
                        NSAttributedString.Key.foregroundColor : UIColor.white,
                        NSAttributedString.Key.font : UIFont(name: "Avenir-Heavy", size: 45)!
                    ]
                    frontNavigationController.navigationItem.leftBarButtonItem?.action = #selector(SWRevealViewController.revealToggle(_:))
                    revealController.frontViewController = frontNavigationController
                    revealController.rearViewController = rear
                    revealController.delegate = self
                    mainRevealController  = revealController

                    UIApplication.shared.delegate!.window??.rootViewController = mainRevealController
                }
            })
        }else{
            let login = storyboard.instantiateViewController(withIdentifier: "LogInController")
            self.window?.rootViewController = login
            self.window?.makeKeyAndVisible()
        }
    }

    return true
}

【讨论】:

    猜你喜欢
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 2016-05-31
    • 2015-01-01
    • 2012-05-18
    • 1970-01-01
    相关资源
    最近更新 更多