Reputation: 127
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:
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 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!
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
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
Reputation: 1697
UITableViewController
in your storyboard.UITableViewController
(the one you want to present modally), and embed it in an UINavigationController
.UIBarButtonItem
into UINavitionItem
of the Second UITableViewController
.UITablViewCell
of your First UITableViewController
Control+Drag
into your UINavigationController
.Present Modally
" under the "Selecteion Segue" option from the dropdown list.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:
This should do the job, Cheers!
Upvotes: 5
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