Ios 无法在UIStoryBoard segue中传递值
我需要根据某些条件将布尔值发送到第二个viewController。 但在第二个ViewController中,bool变量的值始终为false。 这是第一个ViewController代码:Ios 无法在UIStoryBoard segue中传递值,ios,swift,segue,uistoryboard,uistoryboardsegue,Ios,Swift,Segue,Uistoryboard,Uistoryboardsegue,我需要根据某些条件将布尔值发送到第二个viewController。 但在第二个ViewController中,bool变量的值始终为false。 这是第一个ViewController代码: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loadin
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func moveTo(_ sender: Any) {
self.performSegue(withIdentifier: "Move2", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "Move2"
{
let secondVC = segue.destination as? SecondViewController
secondVC?.second = true
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
第二个ViewController代码为
import UIKit
class SecondViewController: UIViewController {
var second = Bool()
override func viewDidLoad() {
super.viewDidLoad()
print(second)
// Do any additional setup after loading the view.
}
但第二个变量的值为false
我的代码有什么问题。我需要帮助。蒂亚
编辑:在两个ViewController中,我都有导航控制器。这就是问题所在。SecondVC是SecondViewController的导航控制器,因此我无法将数据传递给SecondVC。保持简单和干净: FirstVC: 第二个VC:
我自己刚测试过。将打印true。转到故事板,选择左上角的黄色圆圈。转到identity inspector并从类下拉列表中选择SecondViewController 见下图:
你在情节提要中设置了segue标识符了吗?单击segue箭头,并在identify inspector中将segue标识符设置为Move2确保已在Storyboard.ya中设置SecondViewController的类。。设置为Move2keep在secondVC?处保留断点。second=真行。调试在这里停止。将second=Bool更改为second:Bool=false secondVC?.second=true-是可选的如果让secondVC=…,secondVC是可选的如何。@脚本:我看不出有任何可选的需要。让secondVC=segue.destination作为?SecondViewController作为?创建选项您可以更改为?作为!这是一个非常有效的编码决策。如果没有将segue连接到正确的VC类型,您可能希望它崩溃。在两个viewcontroller中,它都包含导航控制器。我在第二个VC中删除了导航控制器。现在工作正常了。。这是问题吗?如果SecondViewController嵌入到导航控制器中,则会导致问题。通常你的第一个VC是嵌入式的,就是这样。
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "Move2"
{
let secondVC = segue.destination as? SecondViewController
secondVC.second = true
}
}
class SecondViewController: UIViewController {
var second: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
print(second)
}
}