简体   繁体   English

视图控制器之间的数据未通过

[英]Data between view controllers not passing

I am creating an application in which there are 6 view controller in storyboard. 我正在创建一个应用程序,其中情节提要中有6个视图控制器。 The thing is that data is shared between the default view controller and the first one ( say A and B) which i added. 问题是默认视图控制器与我添加的第一个视图控制器(例如A和B)之间共享数据。 i am using the prepareforseque method for passing data. 我正在使用prepareforseque方法传递数据。 the problem started when i added two more view controller. 当我再添加两个视图控制器时,问题就开始了。 lets say C and D i created two new swift files and changed the two view controller class name. 假设C和D我创建了两个新的swift文件并更改了两个视图控制器类名称。 i created a textbox and button in C and label in D. when i pressed the button, the value of the text field is not passing into the D view controller although i used the same methods and code which i used for A and B. do i have to do anything else when i want to pass data between two newly added view controller. 我在C中创建了一个文本框和按钮,在D中创建了标签。当我按下按钮时,尽管我使用了与A和B相同的方法和代码,但文本字段的值并未传递到D视图控制器中。当我想在两个新添加的视图控制器之间传递数据时,我必须做任何其他事情。

first viewcontroller in which when a button is pressed value 1 needed to be passed: 第一个viewcontroller,在其中按下按钮时需要传递值1:

class PlaySelectMenu: UIViewController {

var value = Int()

@IBAction func twotofive(sender: AnyObject) {

    value = 1
}

override func viewDidLoad() {
    super.viewDidLoad()

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let nextView : PlayGameView = segue.destinationViewController as! PlayGameView
      nextView.x = value
}
}

the second view controller which receive the value and print it 第二个视图控制器,接收值并打印

import Foundation
import UIKit

class PlayGameView: UIViewController{

var x = Int()
override func viewDidLoad() {
    super.viewDidLoad()
    print(x)
}
}

here i have added both the view controller from the object library and not working with the default one which is present in storyboard by default. 在这里,我已经从对象库中添加了视图控制器,并且无法使用默认情况下存在于情节提要中的默认控制器。 i dont know why these two viewcontroller are not working. 我不知道为什么这两个viewcontroller无法正常工作。 please help. 请帮忙。 Regards Dev 关于开发

One solution would be to write the data out to NSUserDefaults and then read it back from NSUserDefaults in the other view controller. 一种解决方案是将数据写出到NSUserDefaults中,然后从另一个视图控制器中的NSUserDefaults中读回。 Probably not the proper or correct way to share data between two view controllers, but it's been a reliable work around for me. 在两个视图控制器之间共享数据可能不是正确或正确的方法,但这对我来说是一个可靠的解决方法。

Other than that, you'd need to share your code so that we can see what's occurring. 除此之外,您还需要共享您的代码,以便我们可以看到正在发生的事情。

您还可以在控制器C和D中发布代码吗?并且如果您已经将前两个控制器中的代码复制/粘贴到了另外两个控制器中,是否确定在prepareForSegue方法中您已经更改了目标序列的名称?

Assuming you have created the segue in Storyboard: 假设您在情节提要中创建了segue:

All you need is to do is put all of needed updates in prepareForSegue because twotofive is called after prepareForSegue . 您只需要做的就是把所有需要更新的prepareForSegue因为twotofive被称为后prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    value = 1
    let nextView : PlayGameView = segue.destinationViewController as! PlayGameView
    nextView.x = value
}

Since you have connected your segue from button click to view controller, when you press button segue is automatically called. 由于您已通过单击按钮连接到segue,以查看控制器,因此当您按下按钮时,segue将自动被调用。 Instead of connecting segue from button to VC, connect VC to VC. 不用将按钮从按钮连接到VC,而是将VC连接到VC。 Then in button click method at the last add below line: 然后在按钮的单击方法处最后添加以下行:

@IBAction func twotofive(sender: AnyObject) {

    value = 1
    self.performSegueWithIdentifier("<Name of the segue identifier>", sender: self)
}

This will call your prepareForSegue. 这将调用您的prepareForSegue。 If you are calling more then one VC using segue from a VC then you can use segue.identifier to check which VC was called as below 如果要使用一个VC中的segue调用多个VC,则可以使用segue.identifier来检查哪个VC被如下调用

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if segue.identifier == "CVC" {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM