gpt4 book ai didi

ios - 在 swift 中使用 SWReveal 设置 Root View Controller 时出错

转载 作者:行者123 更新时间:2023-11-28 07:20:12 25 4
gpt4 key购买 nike

我[正在尝试根据条件在应用委托(delegate)中设置 Root View Controller 。当应用程序启动并将 Root View Controller 设置为一个 Controller ,该 Controller 具有显示后 View Controller 的代码时出现此错误:“ fatal error :在展开可选值时意外发现 nil”

应用委托(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
}

触发错误的 View Controller 中的代码。

编辑:加载初始 View Controller 时触发错误。初始 View Controller 由应用委托(delegate)中的条件决定

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

最佳答案

我通过在每种情况下设置前后 View 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
}

关于ios - 在 swift 中使用 SWReveal 设置 Root View Controller 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58444230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com