ios - swiftpresentViewController以编程方式在destinationVC VDL中找到nil

标签 ios xcode swift presentviewcontroller

尝试通过了解基础级别的代码行来巩固我的知识。

我已经调整了我的应用程序以使用 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)

    }    
}

知道我错过了什么吗?

enter image description here

enter image description here

注意:顺便说一句,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

然后我们可以使用

来呈现 VC
self.presentViewController(vc, animated: true, completion: nil)

这将导入所有destinationVC的对象及其IBOutlet引用。

关于ios - swiftpresentViewController以编程方式在destinationVC VDL中找到nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54272112/

相关文章:

ios - 将三个动画间隔一秒的最佳方法是什么?

ios - 如何快速修复 'Multiple push notification' 错误?

iphone - 将多维数组中的图像加载到 UIImageView 中

arrays - 如何改变数组中的变量?

iphone - 对象的 iOS 5 'Facial Recognition'

ios - ViewController 不符合协议(protocol) UITableViewDataSource

ios - UICollectionViewCell 在 UICollectionViewReusableView 宽度之后启动

ios - GPPSignIn 验证请求 client_secret?

ios - 在 MKMapView 中围绕用户位置绘制一个半径为 1000m 的圆

iOS9 UIWebView/Chrome CSS 过渡