swift presentViewController以编程方式在destinationVC VDL中找到nil [英] swift presentViewController programmatically finds nil in destinationVC VDL

查看:89
本文介绍了swift presentViewController以编程方式在destinationVC VDL中找到nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过在基本级别上理解代码行来巩固我的知识.

Trying to solidify my knowledge by understanding lines of code at a basic level.

我已经对应用程序进行了调整,以将ViewController(VC)2用作初始VC,而不是VC1.

I've tweaked my app to use ViewController(VC)2 as initial VC instead of VC1.

我正在练习完全通过代码填充此VC2.当对UIButton进行编码以插入VC1时,我的控制台将输出致命错误:在解开Optional值时意外发现nil". (lldb).并且线程1指向VC1 viewDidLoad(VDL),我在其中设置了一些属性

I'm practicing populating this VC2 entirely by code. When coding a UIButton to segue into VC1, my console outputs "fatal error: unexpectedly found nil while unwrapping an Optional value" (lldb). and thread 1 points to VC1 viewDidLoad (VDL) where I've set some properties

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没有任何问题.

This VC1 was not having any problems with VDL when it was the initial VC.

填充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)

    }    
}

知道我想念什么吗?

注意:btw VC2实际上在我的项目中称为设置"

NOTE: btw VC2 is actually called Settings in my project

推荐答案

因此,在实例化一个VC(由storyboard创建的)时,首先我们需要按名称路由通过UIStoryboard的呼叫

So when instantiating a VC (storyboard-created), first we need to route our call through the UIStoryboard by name

let storyboard = UIStoryboard(name: "Main", bundle: nil)  

然后使用我们的情节提要设置VC的情节提要ID实例化VC

then instantiate a VC using the Storyboard ID of our storyboard setup VC

let vc = storyboard.instantiateViewControllerWithIdentifier("VC1") as VC1    //VC1 refers to destinationVC source file and "VC1" refers to destinationVC Storyboard ID

然后我们可以使用

self.presentViewController(vc, animated: true, completion: nil)

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

This will import all of destinationVC's objects with their IBOutlet references.

这篇关于swift presentViewController以编程方式在destinationVC VDL中找到nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆