简体   繁体   English

在 swift 中使用 SWReveal 设置根视图 controller 时出错

[英]Error setting root view controller with SWReveal in swift

I[m trying to set the root view controller in the app delegate based in a condition.我[正在尝试根据条件在应用程序委托中设置根视图 controller。 When the app starts and set the root view controller a controller that has code to reveal the rear ViewController this error: "Fatal error: Unexpectedly found nil while unwrapping an Optional value"当应用程序启动并设置根视图 controller 和 controller 时,有代码显示后视图控制器此错误:“致命错误:在展开可选值时意外发现 nil”

App Delegate应用代理

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
}

code in the view controller where the error is triggered.触发错误的视图 controller 中的代码。

EDIT: error is triggered when the initial view controller is loaded.编辑:加载初始视图 controller 时触发错误。 The initial view controller is determined by condition in the app delegate初始视图 controller 由应用委托中的条件确定

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())
}

I fixed it by setting the front and rear view controller in each condition.我通过在每种情况下设置前后视图 controller 来修复它。 im using a navigation controller so some parts of the code below are properties of a navigarion controller我正在使用导航 controller 所以下面代码的某些部分是导航 controller 的属性

 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM