[英]managing page navigation at swift2
I'm trying to learn page navigation without using "Navigation Controller". 我正在尝试在不使用“导航控制器”的情况下学习页面导航。
Created a button and function like; 创建了一个按钮,功能类似于;
@IBAction func backPressed(sender: AnyObject) {
self.performSegueWithIdentifier("page2ToMain", sender: self)
}
It works fine. 工作正常。
I need to pass data from page 2 to page 3. So, inside "didSelectRowAtIndexPath" I used "performSegueWithIdentifier" and override "prepareForSegue" function as below; 我需要将数据从第2页传递到第3页。因此,在“ didSelectRowAtIndexPath”内部,我使用了“ performSegueWithIdentifier”并覆盖了“ prepareForSegue”函数,如下所示;
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.selectedRow = self.dataArray[indexPath.row]
self.performSegueWithIdentifier("page2ToPage3", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let nextViewController = segue.destinationViewController as! ViewController3
nextViewController.data = self.selectedRow
print(self.selectedRow)
}
When I click the table cell it works well, jumps to other view controller and I see data variable. 当我单击表格单元格时,它运行良好,跳到其他视图控制器,我看到了数据变量。
But when I press back button it fails with; 但是当我按下后退按钮时,它失败了;
Could not cast value of type 'test3.ViewControllerMain' (0x1045469f0) to 'test3.ViewController3' (0x1045466e0).
How can I solve the problem? 我该如何解决这个问题?
You can test for this with optional binding: 您可以使用可选绑定进行测试:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let nextViewController = segue.destinationViewController as? ViewController3 {
nextViewController.data = self.selectedRow
print(self.selectedRow)
}
}
Or look at the segue identifier
and then test for that: 或者查看segue
identifier
,然后进行测试:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "page2ToPage3" {
let nextViewController = segue.destinationViewController as! ViewController3
nextViewController.data = self.selectedRow
print(self.selectedRow)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.