Ios Swift:将(模式)切换到嵌入另一个导航控制器中的UITableViewController

Ios Swift:将(模式)切换到嵌入另一个导航控制器中的UITableViewController,ios,casting,segue,Ios,Casting,Segue,背景:我想显示从UITableViewController(a)到UITableViewController(B)的模式序列,但我想显示一个导航栏以“取消”和“保存”。 我所做的: 在故事板中: 我按ctrl键将单元格从A拖动到B,并设置segue identifer“selectItem” 我选择B并选择“编辑器-嵌入-导航控制器” 在用户的ViewController中: override func prepareForSegue(segue: UIStoryboardSegue, sen

背景:我想显示从UITableViewController(a)到UITableViewController(B)的模式序列,但我想显示一个导航栏以“取消”和“保存”。


我所做的
在故事板中:

  • 我按ctrl键将单元格从A拖动到B,并设置segue identifer“selectItem”
  • 我选择B并选择“编辑器-嵌入-导航控制器”
  • 在用户的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
            }
        }
    }
    


    错误:应用程序在
    将destinationViewController=segue.destinationViewController设为B时崩溃,错误为
    swift\u dynamiccastclass无条件
    。如果我没有在B中嵌入导航控制器,程序就不会崩溃。但我真的需要一个导航栏
    是否有任何解决方案或其他方法来实现这一点?非常感谢。


    PS:我试着从故事板中的对象库中拖动导航栏,但它缺少一部分背景来覆盖状态栏

  • 在情节提要中创建两个
    UITableViewController
  • 选择第二个
    UITableViewController
    (要以模式显示的一个),并将其嵌入到
    UINavigationController
  • 将“取消”和“保存”
    UIBarButtonItem
    添加到第二个
    UITableViewController
    UINavitionItem
  • 选择第一个
    UITableViewController的
    uitableviewcell
  • Control+将
    拖动到
    UINavigationController
  • 从下拉列表中的“Selecteion Segue”选项下选择“
    Present Modally
  • 要在第一个
    UITableViewController
    中传递数据,请覆盖该方法:
  • 以下是截图:

    这应该可以完成任务,干杯

    我解决了
    我在行
    let dest=segue.destinationViewController作为B
    添加了一个断点,发现
    segue.destinationViewController
    NavigationController
    类型。因此,我将此行替换为:

    let dest = segue.destinationViewController as UINavigationController
    let bVC = dest.topViewController as B
    
    做一些传递价值的事情。
    希望这将帮助其他人面对这个问题。

    我使用

    if let b = segue.destinationViewController.childViewControllers.first as? B {
        //do something with b
    }
    

    对于这种情况。这实际上只是语法上的差异。

    我可以继续显示第二个tableViewController,但我不能使用preparedForSegue方法传递值(当我将destinationViewController强制转换为第二个tableVC时)。我刚刚解决了!谢谢你了。
    if let b = segue.destinationViewController.childViewControllers.first as? B {
        //do something with b
    }