【发布时间】:2018-09-21 10:10:19
【问题描述】:
我需要根据某些条件将 bool 值发送到 Second viewController。 但在 Second ViewController 中,布尔变量的值始终为 false。 这是第一个 ViewController 代码:
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.
}
}
SecondViewController 代码是
import UIKit
class SecondViewController: UIViewController {
var second = Bool()
override func viewDidLoad() {
super.viewDidLoad()
print(second)
// Do any additional setup after loading the view.
}
但是第二个变量的这个值是假的。
我的代码有什么问题。需要帮忙。 TIA..
编辑:在两个 ViewController 我都有导航控制器。那是问题。 SecondVC 是 SecondViewController 的导航控制器,所以我不能向 secondVC 传递数据。
【问题讨论】:
-
您是否在 Storyboard 中设置了 segue 标识符?单击 segue 箭头并在识别检查器中将 segue 标识符设置为
Move2 -
确保您的
SecondViewController的class已在情节提要中设置。 -
ya.. 设置为“Move2”
-
在 secondVC?.second = true 行保留一个断点。并且调试是编译器在这里停止。
-
更改
second = Bool() to second: Bool = false
标签: ios swift segue uistoryboard uistoryboardsegue