OzzieChen OzzieChen - 3 months ago 6x
iOS Question

Swift: Segue(modal) to a UITableViewController which embeded in antoher navigation controller

Background: I want to display a modal segue from a UITableViewController(A) to a UITableViewController(B), but I want to show a NavigationBar to "Cancel" and "Save".

What I've done:

In storyboard:

  1. I ctrl drag the cell from A to B, and set segue identifer "selectItem"

  2. I choose B and select "Editor - Embed in - Navigation Controller"

In A's 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


Error:The app crashed at
let destinationViewController = segue.destinationViewController as B
with the error
. If I didn't embed a navigation controller to B, the program would not crash. But I really need a navigation bar.

Is there any solution or other way to achieve this? Thank you!
error 1
enter image description here

PS: I tried drag a NavigationBar from object library in storyboard, but it's miss a part of background to cover statusbar...


I solved it!
I added a BreakPoint at the line let dest = segue.destinationViewController as B, and I found that segue.destinationViewController is NavigationController type. So I fixed it by replacing this line to:

let dest = segue.destinationViewController as UINavigationController
let bVC = dest.topViewController as B

and do some passing value stuff.
Hope this will help other people facing this problem.