简体   繁体   English

当我尝试访问它们时,IBOutlet变量返回nil。 错误:线程1:致命错误:展开一个可选值时意外发现nil

[英]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 . 我有一个实现MGLMapViewDelegateUIViewController 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.

相关问题 解包可选的 IBOutlet 时出现错误(线程 1:致命错误:解包可选值时意外发现 nil) - I have an error when unwrapping an optional IBOutlet (Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value) 在项目中添加了自定义字体,但是当我尝试使用它们时,出现“严重错误:在展开可选值时意外发现nil” - Added custom font to project but when I try and use them I get a “fatal error: unexpectedly found nil while unwrapping an Optional value” 尝试获取联系电话时,出现错误“致命错误:在展开可选值时意外发现nil” - I got an error “fatal error: unexpectedly found nil while unwrapping an Optional value” when I try to get a phone number in contact 当我尝试使用userDefaults保存为ui颜色时,Xcode给我以下错误:致命错误:在展开可选值时意外发现nil - When I try to save to ui colors with userDefaults, Xcode gives me the error : Fatal error: Unexpectedly found nil while unwrapping an Optional value 线程 1:致命错误:在测试 cocoapod 时,在解开 Optional 值时意外发现 nil - Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value when testing cocoapod 线程1:致命错误:当我尝试使用NSuserDefaults保存对象时,在展开可选值时意外发现nil - Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value as I am trying to save an object with NSuserDefaults 运行我的应用程序时出现此错误。 线程1:致命错误:展开一个可选值时意外地找到零 - I get this error when i run my app. Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value 当我实现Parse时,我得到“致命错误:在展开可选值时意外发现nil” - When I implement Parse I get “fatal error: unexpectedly found nil while unwrapping an Optional value” ARQuicklook-致命错误:展开一个可选值时意外发现nil - ARQuicklook - Fatal error: Unexpectedly found nil while unwrapping an Optional value 致命错误:在AVFoundation中解开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in AVFoundation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM