swift presentViewController 以编程方式在 destinationVC VDL 中找到 nil
swift presentViewController programmatically finds nil in destinationVC VDL
试图通过在基本水平上理解代码行来巩固我的知识。
我调整了我的应用程序以使用 ViewController(VC)2 作为初始 VC 而不是 VC1.
我正在练习完全通过代码填充此 VC2。将 UIButton 编码为 segue 到 VC1 时,我的控制台输出“致命错误:在展开可选值时意外发现 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 在我的项目中实际上称为设置
因此在实例化 VC(故事板创建)时,首先我们需要通过名称
通过 UIStoryboard 路由我们的调用
let storyboard = UIStoryboard(name: "Main", bundle: nil)
然后使用我们的情节提要设置的情节提要 ID VC
实例化一个 VC
let vc = storyboard.instantiateViewControllerWithIdentifier("VC1") as VC1 //VC1 refers to destinationVC source file and "VC1" refers to destinationVC Storyboard ID
然后我们可以使用
呈现 VC
self.presentViewController(vc, animated: true, completion: nil)
这将导入目标VC 的所有对象及其 IBOutlet 引用。
试图通过在基本水平上理解代码行来巩固我的知识。
我调整了我的应用程序以使用 ViewController(VC)2 作为初始 VC 而不是 VC1.
我正在练习完全通过代码填充此 VC2。将 UIButton 编码为 segue 到 VC1 时,我的控制台输出“致命错误:在展开可选值时意外发现 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 在我的项目中实际上称为设置
因此在实例化 VC(故事板创建)时,首先我们需要通过名称
通过 UIStoryboard 路由我们的调用let storyboard = UIStoryboard(name: "Main", bundle: nil)
然后使用我们的情节提要设置的情节提要 ID VC
实例化一个 VClet 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)
这将导入目标VC 的所有对象及其 IBOutlet 引用。