Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios viewController的直接初始化_Ios_Swift_Initialization_Viewcontroller - Fatal编程技术网

Ios viewController的直接初始化

Ios viewController的直接初始化,ios,swift,initialization,viewcontroller,Ios,Swift,Initialization,Viewcontroller,我的欲望输出: 我想通过直接初始化第二个viewController来传递数据 在我的第一个vc中: class MainViewController : UIViewController { @IBAction func nextQuestion(_ sender: UIButton ) { //enter code here questionIndex += 1 let currentQuestion = exam1[quest

我的欲望输出:

我想通过直接初始化第二个viewController来传递数据

在我的第一个vc中:

class MainViewController : UIViewController {

    @IBAction func nextQuestion(_ sender: UIButton ) {

        //enter code here
         questionIndex += 1
         let currentQuestion = exam1[questionIndex]
         questionLabel.text = currentQuestion.question
        //enter code here
        let svc = SecondViewController()
        svc.label.text = "some data from mainvc"  
}
在我的第二份报告中:

class SecondViewController : UIViewController {
    @IBOutlet weak var label : UILabel! 
}

//当我在模拟器工作时编写代码时,没有显示任何错误。但是,当向svc应用程序发送数据时,会崩溃并给出错误“在展开可选文件时发现零”

问题在于,当您这样做时:

让svc=SecondViewController()

您在初始化时没有将信息放入序列图像板(假设您使用的是序列图像板)。因此,UILabel永远不会初始化

解决方案

为了解决此问题,请更换管路:

让svc=SecondViewController()

为了


让svc=UIStoryboard(名称:“YourStoryboardName”,bundle:nil)。将eviewController(带标识符:“YourViewIdentifier”)实例化为!SecondViewController

可能重复,因为您的
标签
nil
。但我在secondvc中定义了它。如果我将其定义为let labelText=label.text,并给labelText一个字符串值。在应用了我给你的解决方案后,下一个问题是视图没有出现,这是否有效?是吗?如果您在查找UIViewController标识符时遇到任何问题,请告诉我,当我编写代码时,它会给出错误,因为一行上的连续声明必须分开。因为SecondViewController()在修复SecondViewController时;()代码不再工作。İ无法将单个数据传递给下一个vc。这是为什么。İvan和segue一起做。但我必须在两个vc之间不使用segue或连接的情况下执行此操作。当触发一个按钮时,我想将数据发送到下一个vc。但是我不想看到下一个vc,因为我希望按钮可以执行其他任务,同时向下一个vc发送数据。有可能吗?我有两个视图控制器和选项卡栏控制器,所以vc之间没有连接。只是一个简单的测验或考试应用程序。当我按下下一个问题按钮时,会出现下一个问题的字符串,这是确定的。但当要将答案数据发送到下一个vc时,问题就出现了。@SammHadji回答您的第一个评论。是的,您必须为我发布的代码替换
let svc=SecondViewController()