Swift:Segue(模态)到嵌入antoher导航控制器的UITableViewController

Ozz*_*hen 4 casting ios segue

背景:我想显示从UITableViewController(A)到UITableViewController(B)的模态segue,但我想显示一个NavigationBar为"Cancel"和"Save".


我做了什么:
在故事板中:

  1. 我按住c将单元格从A拖动到B,然后设置segue标识符"selectItem"
  2. 我选择B并选择"编辑器 - 嵌入 - 导航控制器"

在A的ViewController中:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifer == "selectItem" {
        if let indexPath = self.tableView.indexPathForSelectedRow() {
            let destinationViewController = segue.destinationViewController as B
            // Pass value from A to B
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

故事板


错误:应用程序let destinationViewController = segue.destinationViewController as B因错误而崩溃swift_dynamicCastClassUnconditional.如果我没有将导航控制器嵌入到B中,程序就不会崩溃.但我真的需要一个导航栏.
有没有解决方案或其他方法来实现这一目标?谢谢! 错误1 在此输入图像描述


PS:我试过在故事板中从对象库中拖动一个NavigationBar,但它错过了背景的一部分来覆盖状态栏...

Sha*_*bib 5

  1. UITableViewController在故事板中创建两个.
  2. 选择你的第二个UITableViewController(你想以模态方式呈现的那个),并将其嵌入到UINavigationController.
  3. 添加" 取消 "和" 保存 " UIBarButtonItem到UINavitionItem第二个UITableViewController.
  4. 选择UITablViewCell你的第一个UITableViewController
  5. Control+Drag进入你的UINavigationController.
  6. Present Modally从下拉列表中的"Selecteion Segue"选项下选择" ".
  7. 要在First中传递数据,请UITableViewController覆盖以下方法:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "identifier" {
            let destination = segue.destinationViewController as UINavigationController
            let bViewController = destination.topViewController as BViewController

            // pass data
        }
}
Run Code Online (Sandbox Code Playgroud)

以下是截图:

在此输入图像描述 在此输入图像描述

这应该做的,干杯!