使用SWReveal快速设置根视图控制器时出错

问题描述 投票:1回答:1

我[正在尝试根据条件在应用程序委托中设置根视图控制器。当应用启动并设置根视图控制器时,具有代码的控制器可以显示后方ViewController的此错误:“致命错误:在展开可选值时意外发现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())
}
ios swift firebase swrevealviewcontroller
1个回答
0
投票

我通过在每种情况下都设置前视图和后视图控制器来修复它。即时通讯使用导航控制器,因此以下代码的某些部分是navigarion控制器的属性]

 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
}
© www.soinside.com 2019 - 2024. All rights reserved.