Destinationviewcontroller Segue and Uinavigationcontroller Swift

DestinationViewController Segue and UINavigationController swift

You only need to access the navigation controller's topViewController, which will be the AddEventViewController. Here is the code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "fromEventTableToAddEvent" {

let nav = segue.destinationViewController as! UINavigationController
let addEventViewController = nav.topViewController as! AddEventViewController

addEventViewController.newTagArray = newTagArray

}
}

SWIFT 3: Segue with navigation controller

For the first Problem, When using embedded in navigation controller you have don't have to create action function when the button is tapped. Navigation controller does it for you. So things you need to do :

  1. Disconnect the function btnTapped from the button using storyBoard.
  2. Delete or comment the function btnTapped(You don't need it).

Set data in `prepareForSegue` with navigation controller

In prepareForSegue access the target navigation controller, and then its top:

let destinationNavigationController = segue.destination as! UINavigationController
let targetController = destinationNavigationController.topViewController

From the target controller you can access its view and pass data.

In old - now obsolete - versions of Swift and UIKit the code was slightly different:

let destinationNavigationController = segue.destinationViewController as UINavigationController
let targetController = destinationNavigationController.topViewController

Passing data with segue through navigationController

The destination view controller of the segue is the UINavigationController. You need to ask it for its top view controller to get to the real destination view controller:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showItemSegue" {
let navController = segue.destination as! UINavigationController
let detailController = navController.topViewController as! ShowItemViewController
detailController.currentId = nextId!
}
}


Related Topics



Leave a reply



Submit