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:
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
How to Properly Animate Uiscrollview Contentoffset
How to Animate Uitableviewcell Height Using Auto-Layout
Saving Attributes on a User Fetched from a Query (I.E. Not on the Currentuser)
Swift 3: How to Add Watermark on Video? Avvideocompositioncoreanimationtool iOS 10 Issue
How to Dynamically Format a Number to Have Commas in a Uitextfield Entry
Cllocationmanager Responsiveness
How to Use Uiviewcontrolleranimatedtransitioning with Uinavigationcontroller
Swift - Must Call a Designated Initializer of the Superclass Skspritenode Error
What Is the Impact of the "Requires Full Screen" Option in Xcode for an Iphone-Only App
Container View Getting Pushed Down as If It Had a Uinavigationbar
How Should a Swift + Objective-C Project Be Setup for Unit Testing
Change App Language in iOS Without Restarting the App
What Is Main Thread Checker in Xcode
How to Add a Character at a Particular Index in String in Swift
Get Current iOS Device Orientation Even If Device's Orientation Locked
Swiftui in iOS14 Keyboard Avoidance Issues and Ignoressafearea Modifier Issues