OzzieChen
OzzieChen

Reputation: 127

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
        }
    }
}

storyboard


Error:The app crashed at let destinationViewController = segue.destinationViewController as B with the error swift_dynamicCastClassUnconditional. 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...

Upvotes: 4

Views: 5539

Answers (3)

Sethmr
Sethmr

Reputation: 3074

I use

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

for this scenario. It is really just a syntax difference though.

Upvotes: 0

Shabib
Shabib

Reputation: 1697

  1. Create both of the UITableViewController in your storyboard.
  2. Select your Second UITableViewController (the one you want to present modally), and embed it in an UINavigationController.
  3. Add the "Cancel" and "Save" UIBarButtonItem into UINavitionItem of the Second UITableViewController.
  4. Select the UITablViewCell of your First UITableViewController
  5. Control+Drag into your UINavigationController.
  6. Select "Present Modally" under the "Selecteion Segue" option from the dropdown list.
  7. To pass data in your First UITableViewController override the method:

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
        }
}

Here are the screenshots:

enter image description here enter image description here

This should do the job, Cheers!

Upvotes: 5

OzzieChen
OzzieChen

Reputation: 127

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.

Upvotes: 0

Related Questions