Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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 didSelectRowAtIndexPath performSegueWithIdentifier=我在目标视图中得到零_Ios_Swift - Fatal编程技术网

Ios didSelectRowAtIndexPath performSegueWithIdentifier=我在目标视图中得到零

Ios didSelectRowAtIndexPath performSegueWithIdentifier=我在目标视图中得到零,ios,swift,Ios,Swift,我的“开始”视图中有第三个视图 我有一个序列标识符:restDetail 当我运行一个项目并单击单元格时,我无法在“第二”视图中接收变量valuePassed,我得到零。请帮忙,为什么? 但是我通常从函数prepareforsgue接收一个变量programVar,这是正常的。我对didSelectRowAtIndexPath segue只有一个问题 这是我的第四个观点: import UIKit class FourthView: UIViewController { var valu

我的“开始”视图中有第三个视图

我有一个序列标识符:restDetail

当我运行一个项目并单击单元格时,我无法在“第二”视图中接收变量valuePassed,我得到零。请帮忙,为什么? 但是我通常从函数prepareforsgue接收一个变量programVar,这是正常的。我对didSelectRowAtIndexPath segue只有一个问题

这是我的第四个观点:

import UIKit

class FourthView: UIViewController {


var valuePassed:String!
var programVar:String!

override func viewDidLoad() {


    super.viewDidLoad()
    println(valuePassed);
    println(programVar);


    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
请参见我的输出:

nil
lol
nil
lol

第二个问题:为什么在输出中显示4次?
对不起,我的英语不好。

中选择了rowatinexpath
您没有正确初始化FourthView实例

这一行:

let destinationVC=FourthView()

创建
FourthView
的随机实例,该实例不是您要用作目标的实例。 如果要将值传递给第四视图,通常最好在
prepareforsgue
中进行传递

正如您在下面的一行中所看到的,这里您将destinationVC变量设置为FourthView instance,它是segue的目标视图控制器


让destinationVC=segue.destinationViewController为!第四视图

问题在于以下代码:

let valueToPass = "asd";
let destinationVC = FourthView()
destinationVC.valuePassed = valueToPass;

您正在分配
FourthView
的临时实例,并向其传递数据。然后你表演一段。执行segue初始化视图控制器并将其加载到视图中(不同的视图,而不是在
didselectRowatinedexpath
中手动初始化的视图)。您需要通过
prepareforsgue:
方法传递数据

对于有相同问题的人,请查看需要添加到prepareforsgue()函数中的内容(在我的示例中):
让indexPath:NSIndexPath=self.tableView.indexPathForSelectedRow()destinationVC.name=self.jsonz[indexPath.row]as?弦。在FourthView中,我声明字符串名,并且它可以发送。
let valueToPass = "asd";
let destinationVC = FourthView()
destinationVC.valuePassed = valueToPass;