简体   繁体   English

添加UINavigation时,“在展开可选值时意外发现nil”

[英]“Unexpectedly found nil while unwrapping an Optional value” When UINavigation is added

I was making this app (Swift 4) and it worked just fine. 我当时正在制作这个应用程式(Swift 4),运作正常。
But then , I added another page which was supposed work as the Main Menu(when you open the app , you see that) so therefore , I made another class which was a UINavigationController class(It wasn't the View Controller) but when I use a UIImage , I get "Unexpectedly found nil while unwrapping an Optional value". 但是,然后,我添加了另一个页面,该页面应该作为主菜单工作(当您打开应用程序时,您会看到),因此,我创建了另一个类,它是UINavigationController类(不是View Controller),但是当我使用UIImage,我得到“在展开可选值时意外发现nil”。
My code : 我的代码:

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)


    }
}

(This is the file for the Main Menu) (这是主菜单的文件)

EDIT 1: 编辑1:

The problem was that I should have changed UINavigationController to UINavigationController but now , there is another problem : the "playButton" is supposed navigate to another page but when I press it , again , I get "Unexpectedly found nil while unwrapping an Optional value". 问题是我应该将UINavigationController更改为UINavigationController但是现在,还有另一个问题:应该将“ playButton”导航到另一个页面,但是当我再次按下它时,我得到“在解开一个Optional值时意外地找到了nil”。

Edit 2 : So , I solved both of the problems but now , in another page I have this function : 编辑2:所以,我解决了两个问题,但现在,在另一页中,我具有以下功能:

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

}

but when the function is being run , instead of going to the main page (MainNavigation) , It gives the same problem for the "play button". 但是当函数运行时,而不是转到主页(MainNavigation)时,“播放按钮”也有同样的问题。 Why? 为什么?

Why are you attaching view objects to your navigation controller? 为什么将视图对象附加到导航控制器? You want to push view controllers onto your navigation controller's stack, but it's not normal to put views in your navigation controller. 您想将视图控制器推入导航控制器的堆栈中,但是将视图放入导航控制器是不正常的。

That's likely your problem. 那可能是您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 展开Optional值时意外发现nil - unexpectedly found nil while unwrapping an Optional value 展开可选的value()时意外找到nil - Unexpectedly found nil while unwrapping an Optional value() 加载tableView时解开可选值时意外发现nil - Unexpectedly found nil while unwrapping an optional value when loading tableView 展开Optional值时意外发现nil - Unexpectedly found nil when unwrapping an Optional value 值不是nil,但是在展开Optional值时意外发现nil - value is not nil but getting 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” 打开UIViewController时解开Optional值时意外发现nil - Unexpectedly found nil while unwrapping an Optional value while opening UIViewController 展开可选值时意外发现nil,但是有一些值 - unexpectedly found nil while unwrapping optional value, but there is some value 迅捷的Xcode:解开可选值(lldb)时意外发现nil - swift Xcode: unexpectedly found nil while unwrapping an Optional value (lldb) ARQuicklook-致命错误:展开一个可选值时意外发现nil - ARQuicklook - Fatal error: Unexpectedly found nil while unwrapping an Optional value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM