我试图使用以下函数将数据传递给ViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? ContactTableViewController
{
vc.invitationType = type
print(vc.invitationType)
}
}
它被称为好的,我可以打印
vc.invitationType
,以便在新vc中设置该变量。
问题是什么时候发生的。在新的视图控制器中
print(inv)
没有被执行,因为
invitationType
为零:
class ContactTableViewController: UITableViewController {
var invitationType: InvitationType?
override func viewDidLoad() {
super.viewDidLoad()
if let inv = invitationType {
print(inv)
}
}
}
有人能解释为什么会这样吗?
编辑:
我原以为我在使用故事板上定义的segue,但事实证明,我错误地使用了我的一个函数,该函数实例化了另一个ViewController:
let newVC = originVC.storyboard?.instantiateViewController(withIdentifier: viewcontroller) as! VC
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = newVC
所以问题是我最终设置了变量
邀请类型
在一个实例中,并切换到
ContactTableViewController
.