Passing Data with Segue Through Navigationcontroller

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

Passing Data between view Controllers Using a segue from a view embedded in a navigation controller to a tabbarcontroller

This is my view controller where you can check that I am sending 5 to tabbar first viewcontroller:

   class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.performSegue(withIdentifier: "segueIdentifier", sender: self)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let barViewControllers = segue.destination as! UITabBarController
let destinationNv = barViewControllers.viewControllers?[0] as! UINavigationController
let destinationViewController = destinationNv.viewControllers[0] as! FirstViewController
destinationViewController.currentBalance = 5
}
}

Now You can check my firstview controller where you can check that what value we are getting.

class FirstViewController: UIViewController {

var currentBalance = 0
override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
print(currentBalance)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Now, You can check my console and storyboard:
Sample Image

Sample Image

Pass data from one controller to another via segue and Navigation on different storyboard, Swift, iOS, Xcode

You must take into account the navigation controller in the next storyboard, so your prepare(for: sender:) function should look like this:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = (segue.destination as! UINavigationController).topViewController as! DestinationViewController
vc!.projectsSentT = ProjecctProd
vc!.logedInAsT = "Prod"
}

where DestinationViewController is the name of the second view controller embedded in the navigationController. And use segue.destination not and instance of the viewController directly.

How to pass data from view controller to a navigation controller and then to another view controller?

You can use this in First ViewController:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "presentPopup"
{
let destViewController = segue.destination as! NavigationViewController
let secondViewcontroller = destViewController.viewcontrollers.first as! SecondViewcontroller
secondViewcontroller.myData2 = myData
}
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}

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 between a ViewController to another ViewController with navigation controller Swift 5

If you are using segue, then add "segue_identifier" in storyboard and the in secondviewcontroller add:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue_ identifier" {
let mainTab = segue.destination as! tabBarController
let nav = mainTab.viewControllers[0] as! UINavigationController
let vc = nav.viewControllers.first as! HomeViewController
vc.cid = cityId
}
}

Because your segue destination is UINavigationController, you need to send data to view controller like this

Data not being passed through Segue

Yes, when your viewController is embedded in a UINavigationController you have to work through the parts of the chain until you get to the controller you want. Something like this should work:

override function prepare(for segue: UIStoryBoardSegue, sender: Any?) {
if segue.identifier == “TrackDetailVC” {
if let nav = segue.destinationVC as? UINavigationController {
if let detailsVC = nav.viewControllers[0] as? TrackDetailVC {
if let tr = sender as? newTracks {
detailsVC.track = tr
}
}
}
}
}

Passing data to another ViewController with a NavigationController in the way

If you want to refer to a view controller embedded in a navigation bar, you could try something like:

let destinationController = segue.destinationViewController.childViewControllers.first! as! SomeViewController



Related Topics



Leave a reply



Submit