本文介绍了当我尝试访问它们时,IBOutlet变量返回nil.错误:线程1:致命错误:展开一个可选值时意外发现nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现MGLMapViewDelegateUIViewController.我正在尝试在导航栏的顶部放置两个按钮.

I have a UIViewController that implements MGLMapViewDelegate. 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

这样做时,我收到了错误

When I did this, I received the error

我不知道IBOutlet变量怎么可能为零,我以为它们已分配到情节提要按钮上

I don't understand how the IBOutlet variables can ever be nil, I thought they were assigned to the storyboard buttons

我的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)
  }
}


我认为故事板的一些屏幕截图可能很重要

我已尝试将其放在我的视图中控制器紧接在super.viewDidLoad()

self.storyboard?.instantiateViewController(withIdentifier: "mainViewController") as! ViewController

我尝试将视图移到mapview的前面,而不是主视图

I've tried bringing the view to the front of the mapview instead of the main view

self.mapView.bringViewToFront(navBar)

我尝试将代码放在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

这只会实例化ViewController,但不会实例化故事板上的任何对象.

This will just instantiate the ViewController but not any objects from the story board.

使用以下代码从情节提要中实例化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)
    }

如果您使用多个故事板,

 let storyboard = UIStoryboard(name: "Main", bundle: nil)
 if let vc = storyboard.instantiateViewController(withIdentifier: "mainViewController") as? ViewController {
        self.present(vc, animated: true, completion: nil)
   }

这篇关于当我尝试访问它们时,IBOutlet变量返回nil.错误:线程1:致命错误:展开一个可选值时意外发现nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:50