[英]IBOutlet variables return nil when I try to access them. Error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
I have a UIViewController
that implements MGLMapViewDelegate
. 我有一个实现MGLMapViewDelegate
的UIViewController
。 I'm trying to place a nav bar at the top, with two buttons. 我正在尝试在导航栏的顶部放置两个按钮。
I couldn't get my buttons or nav bar visible so I tried using the function view.bringSubviewToFront()
within my viewDidLoadFunction()
as suggested on this post 我不能让我的按钮或因此我尝试使用功能导航栏可见view.bringSubviewToFront()
我的内viewDidLoadFunction()
上的建议这篇文章
When I did this, I received the error 当我这样做时,我收到了错误
Error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value 错误:线程1:致命错误:展开一个可选值时意外发现nil
I don't understand how the IBOutlet
variables can ever be nil, I thought they were assigned to the storyboard buttons 我不知道IBOutlet
变量怎么可能为零,我认为它们已分配给情节提要按钮
My ViewController Class(At least the parts that I think are important) 我的ViewController类(至少我认为重要的部分)
class ViewController: UIViewController, MGLMapViewDelegate {
...
var mapView: MGLMapView?
@IBOutlet var navBar: UINavigationBar!
@IBOutlet var signOutButton: UIButton!
@IBOutlet var newPostButton: UIButton!
...
override func viewDidLoad() {
super.viewDidLoad()
self.mapView = MGLMapView(frame: view.bounds, styleURL: MGLStyle.lightStyleURL)
...
view.addSubview(self.mapView!)
view.addSubview(navBar)
view.addSubview(signOutButton)
view.addSubview(newPostButton)
//Make the navBar and Buttons visible
view.bringSubviewToFront(self.navBar)
view.bringSubviewToFront(self.signOutButton)
view.bringSubviewToFront(self.newPostButton)
...
}
...
@objc func handleLogout() {
let loginController = SignInViewController()
do {
try Auth.auth().signOut()
} catch let logoutError {
print(logoutError)
}
present(loginController, animated: true, completion: nil)
}
}
Some screenshots of my storyboard I thought might be important 我认为故事板的一些屏幕截图可能很重要
I've tried placing this in my view controller right after super.viewDidLoad() 我尝试将其放置在super.viewDidLoad()之后的视图控制器中
self.storyboard?.instantiateViewController(withIdentifier: "mainViewController") as! ViewController
I've tried bringing the view to the front of the mapview instead of the main view 我尝试将视图带到mapview的前面,而不是主视图
self.mapView.bringViewToFront(navBar)
I've tried placing the code inside functions like awakeFromNib and viewDidLayoutSubviews 我试过将代码放在awakeFromNib和viewDidLayoutSubviews之类的函数中
override func awakeFromNib() {
super.awakeFromNib()
//Make the navBar and Buttons visible
self.mapView!.bringSubviewToFront(self.navBar)
self.mapView!.bringSubviewToFront(self.signOutButton)
self.mapView!.bringSubviewToFront(self.newPostButton)
}
From your comments, I understood that you are instantiating the controller wrongly. 从您的评论中,我了解到您错误地实例化了控制器。
let mainController = ViewController(). // Wrong
This will just instantiate the ViewController but not any objects from the story board. 这将只实例化ViewController,但不会实例化故事板上的任何对象。
Use below code to instantiate viewcontroller from storyboard. 使用以下代码从情节提要中实例化viewcontroller。
if let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainViewController") as? ViewController {
//Set any data if required, then present
self.present(vc, animated: true, completion: nil)
}
If you use multiple storyBoards, 如果您使用多个StoryBoard,
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let vc = storyboard.instantiateViewController(withIdentifier: "mainViewController") as? ViewController {
self.present(vc, animated: true, completion: nil)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.