我正在制作这个应用程序(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/