ios - "Unexpectedly found nil while unwrapping an Optional value"添加导航时

标签 ios swift uinavigationcontroller

我正在制作这个应用程序(Swift 4),它运行得很好。
但是然后,我添加了另一个页面,该页面应该作为主菜单(当您打开应用程序时,您会看到),因此,我创建了另一个类,它是 UINavigationController 类(它不是 View Controller ),但是当我使用 UIImage ,我得到“在展开可选值时意外发现 nil”。
我的代码:

import Foundation
import UIKit
class MainNavigation: UIVewController {
    // Screen width.
    public var screenWidth1: CGFloat {
        return UIScreen.main.bounds.width
    }

    // Screen height.
    public var screenHeight1: CGFloat {
        return UIScreen.main.bounds.height
    }


    @IBOutlet weak var backGround: UIImageView!
    @IBOutlet weak var playButton: UIButton!
    @IBOutlet weak var logo: UIImageView!

    @IBOutlet weak var settingButton: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()

        self.backGround?.frame = CGRect(x: 0, y: 0, width: screenWidth1, height: screenHeight1)
        self.logo.frame = CGRect(x: screenWidth1 * 1 / 2 - screenWidth1 / 6, y: 0, width: screenWidth1 / 3, height: screenWidth1 / 3)


    }
}

(这是主菜单的文件)

编辑1:

问题是我应该将 UINavigationController 更改为 UINavigationController 但现在,还有另一个问题:“playButton”应该导航到另一个页面,但当我按下时再次,我得到“在展开可选值时意外发现 nil”。

编辑2: 所以,我解决了这两个问题,但现在,在另一个页面中我有这个功能:

func goToMain(){
    self.present(MainNavigation(), animated: true, completion: nil)

}

但是当该功能正在运行时,而不是转到主页(MainNavigation),它给“播放按钮”带来了同样的问题。 为什么?

最佳答案

为什么要将 View 对象附加到导航 Controller ?您希望将 View Controller 推送到导航 Controller 的堆栈中,但将 View 放入导航 Controller 中是不正常的。

这可能是你的问题。

关于ios - "Unexpectedly found nil while unwrapping an Optional value"添加导航时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50429187/

相关文章:

objective-c - 在 UITableView 中实现图片异步加载的最佳方法

ios - SWIFT - UIDatePicker - 开始和结束日期选择,但无法弄清楚如何将这两个日期调用到两个不同的文本框中

ios - 如何更改 App store 上 iphone 应用程序的设备兼容性?

iphone - 导航 Controller 上的编辑按钮不进入编辑模式

ios - 在 ParallaxBlur Controller 中刷新 TableView

ios - iPhone 和 iPad 上的照明传感器是否会自动调整 RGB 值?

iphone - 如何创建五个带标签的 UIPickerView?以及如何在 iPhone 中为每个 UIPickerView 赋予不同的名称?

ios - 使用 Swift 将 JSON 字符串转为 NSDictionary

ios - 将应用程序从后台转到前台时无法触发 didReceive 通知委托(delegate)

ios - 使用 ios 和 Storyboard编程 UIViewControllers 周期