尝试通过了解基础级别的代码行来巩固我的知识。
我已经调整了我的应用程序以使用 ViewController(VC)2 作为初始 VC 而不是 VC1。
我正在练习完全通过代码填充这个 VC2。当编写一个 UIButton 编码以插入 VC1 时,我的控制台输出“ fatal error :在展开可选值时意外发现 nil”(lldb)。线程 1 指向 VC1 viewDidLoad (VDL),我在其中设置了一些属性
VC1
override func viewDidLoad() {
super.viewDidLoad()
P1Chip.hidden = true; P2Chip.hidden = true; P3Chip.hidden = true; P4Chip.hidden = true; P5Chip.hidden = true; P6Chip.hidden = true; P7Chip.hidden = true; P8Chip.hidden = true; P9Chip.hidden = true
etc
此 VC1 在作为初始 VC 时,VDL 没有任何问题。
填充 VC2 的方法
import Foundation
import UIKit
class VC2: UIViewController {
let home:UIButton! = UIButton(frame: CGRectMake(10,10,15,15)) as UIButton
override func viewDidLoad() {
super.viewDidLoad()
home.backgroundColor = UIColor(red: 0.4, green: 0.6, blue: 0, alpha: 1)
home.setTitle("home", forState: .Normal)
home.addTarget(self, action: "home:", forControlEvents: .TouchUpInside)
view.addSubview(home)
}
func home(sender:UIButton!) {
self.presentViewController((VC1() as UIViewController), animated: true, completion: nil)
}
}
知道我错过了什么吗?
注意:顺便说一句,VC2 在我的项目中实际上称为“Settings”
最佳答案
因此,在实例化 VC(由 Storyboard创建)时,首先我们需要按名称通过 UIStoryboard 路由调用
let storyboard = UIStoryboard(name: "Main", bundle: nil)
然后使用 Storyboard设置 VC 的 Storyboard ID 实例化 VC
let vc = storyboard.instantiateViewControllerWithIdentifier("VC1") as VC1 //VC1 refers to destinationVC source file and "VC1" refers to destinationVC Storyboard ID
然后我们可以使用
来呈现 VCself.presentViewController(vc, animated: true, completion: nil)
这将导入所有destinationVC的对象及其IBOutlet引用。
关于ios - swiftpresentViewController以编程方式在destinationVC VDL中找到nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54272112/