prepareForSegue not working with a Navigation Controller - ios

I have a View Controller with an activity indicator to get data from my database. Once all the data is fetched, this view will performSegueWithIdentifier and use prepareForSegue to pass the data to a TableViewController. The issue that I am experiencing is that if I add a Navigation Controller in between the view that fetches the data and the TableViewController getting the data prepareForSegue does not work, I don't see the data on the TableViewController. However, if I remove the Navigation Controller I get the data just fine. Below is my prepareForSegue method. I am embedding a Navigation Controller there in order to get the transition effect I want with this segue which should have the TableViewController slide in from the left with a push segue. Thanks
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "serviceView" {
if let destination = segue.destinationViewController as? ServiceTableViewController {
destination.firstName = self.firstName
destination.lastName = self.lastName
destination.email = self.email
destination.streetAddress = self.streetAddress
destination.aptOrSuite = self.aptOrSuite
destination.state = self.state
destination.city = self.city
destination.zipcode = self.zipcode
destination.phone = self.phone
}
}
}

embed the view controller that fetches the data in a navigation controller not the table view controller, so both view controllers are in the navigation controller's stack of view controllers

First you need to access the NavigationController, then the topViewController, casting topViewController as your destination NavigationController
let destinationNavigationController = segue.destination as! UINavigationController
let targetController = destinationNavigationController.topViewController as! serviceView
targetController.firstName = self.firstName

Related

add navigation bar but there is no back button

I created a table view and from there let say a user pressed a cell it will go to ListTavleView but the only problem right now is that whenever a user is in ListTableView there is not back button even thought i already embed a navigation controller
and i want the fist view navigation bar is small title second view navigation bar is large title
enter image description here
Below is my code
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showList" {
if let indexPath = tableView.indexPathForSelectedRow {
let items = dataManager.items[indexPath.row]
let controller = (segue.destination as! UINavigationController).topViewController as! ListTableViewController
controller.item = items
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
Below is my storybord setup
Navigation bar with no back button
From the image it seems that view controller is added as a child view controller in current view controller.
There is not need to embedded navigation controller when a cell is pressed becoz there is already a navigation controller at start point so no need to create a new one.(If you present a view controller then you may need to embed navigation controller.)
So the solution is...
Delete the navigation controller.
Connect directly to the destination view controller without navigation controller as there is already.
it is better if you use pushViewController, just get a reference of the other view controller, it will always a back button since you are pushing threw navigation Controller here is a simple example:
let story = UIStoryboard(name: "Main", bundle: nil)
let vc = story.instantiateViewController(withIdentifier: "ExampleViewController") as! ExampleViewController
self.navigationController?.pushViewController(vc, animated: true)
as for the back button, the issue is with your hierarchy.
are you changing the left item of navigation bar in another view controller that might affect navigation bar in your destination view controller.
You are pushing new NavigationController(say Nav.B) to the existing one(Nav.A).
Each navigation controller keeps different navigation stack. The back button is visible when you add viewcontroller to Navigation controller. Read more about UINavigationController.
For your current scenario, you could delete the second navigation controller(i think it not essential) & connect direct segue to ListTableViewController
So this
let controller = (segue.destination as! UINavigationController).topViewController as! ListTableViewController
becomes
let controller = segue.destination as! ListTableViewController
When you need large titles(available 11+), you can add this line in viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
And if it needed only for this Viewcontroller, add in viewWillDisappear() or viewDidDisappear()
navigationController?.navigationBar.prefersLargeTitles = false
If you wanted to have navigation bar back button on next view, then just push the target view on navigation, it will show default navigation back button. No, need to any extra work.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showList" {
if let indexPath = tableView.indexPathForSelectedRow {
let items = dataManager.items[indexPath.row]
guard let controller = segue.destination as? ListTableViewController else {
return
}
controller.item = items
self.navigationController?.pushViewController(controller, animated: true)
}
}}
And if you are pushing the viewcontroller with segue, then no need to add below line self.navigationController?.pushViewController(controller, animated: true)

how send data to new view [duplicate]

This question already has answers here:
Sending data with Segue with Swift
(5 answers)
Closed 6 years ago.
please help. I need send data from view to new view. I have that StoryBoard.
StoryBoard
In first view i have code
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "LEDChangesSegue" {
let changeLED = segue.destinationViewController as? AddLED
changeLED?.senderCell = sender as? LEDListCell
changeLED?.ledController = sender!.ledController
changeLED?.update = true
}
}
this method worked when I use push segue, but now I use modal segue and in segue.destinationViewController
is NavigationController, not my TableViewController
Each navigation controller has array of presented controllers. As well if your Controller is on top(is shown right now) you can use this code to get it navigationController.topViewController
Otherwise you need to go through all controllers and get yours using this code
let navController: UINavigationViewController = segue. destinationViewController
for let controller in navController.viewControllers {
if let neededController = controller as? YourControllerType {
// your action
}
}
As well you can create your custom segue with IBInspectable property of UIViewController that will store your controller inside. After that you can cast this your segue to your custom segue and get your controller from that property.
Use the new destination as VC to call your next tableViewController
let destination = segue.destinationViewController
if let navcon = destination as? UINavigationController {
destination = navcon.visibleViewController!
}
if let hvc = destination as? TableViewController{
// do whatever
}

Swift - Could not cast value of type 'UITabBarController'

I have a login screen that takes credentials, and based on outcome of verification, user is redirected to another view controller or not. The login view controller is controlled by NavigationController and once the login is successful, user is redirected to a Home view controller controlled by UITabBarController.
When I trigger the segue, it says the following error:
Could not cast value of type 'UITabBarController' (0x10d414030) to
'Mawq.HomeViewController' (0x10a7ed220).
Here is the code for the segue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "showHomeFromLoginSegue") {
/*Provide ServiceToken a value from API*/
// pass data to next view
let destinationVC = segue.destinationViewController as! HomeViewController
destinationVC.userObject = self.userObject;
}
}
Any idea how to overcome this issue?
Since your segue is connected to a UITabBarController, you need to cast it to UITabBarController and get the ViewController object using the viewControllers property of the tab bar controller.
let tabCtrl = segue.destinationViewController as! UITabBarController
let destinationVC = tabCtrl.viewControllers![0] as! HomeViewController // Assuming home view controller is in the first tab, else update the array index
destinationVC.userObject = self.userObject;
For Swift 4:
let tabCtrl: UITabBarController = segue.destination as! UITabBarController
let destinationVC = tabCtrl.viewControllers![0] as! HomeViewController
destinationVC.userObject = userObject[String!] // In case you are using an array or something else in the object

Passing an NSManagedObject Effectively through Storyboard

I have quite a complicated setup in my app that involves an embedded UISplitViewController which feeds a bunch of UIViewControllers.
What I am needing to do is pass an NSManagedObject through the embedded UISplitViewController so I can access it in my separate detail' UIViewControllers.
I have attached an image of my storyboard with some awesome annotations....
This is my prepareForSegue function at the minute, this is in the View Controller where I already have my NSManagedObject, I am looking to pass it to the first SurveyViewController so it can get passed on:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
println("Sender is \(sender)")
if segue.identifier == SurveyIdentifier {
// let nav : UINavigationController = segue.destinationViewController as! UINavigationController
// let itemVC: BACUploaderViewController = nav.topViewController as! BACUploaderViewController
let surveyViewController: SurveyViewController = segue.destinationViewController as! SurveyViewController
println("survey view controller: \(surveyViewController)")
// if let destination = segue.destinationViewController as? MasterViewController {
//
// destination.workItem = sender as? Work
// }
}
}
I am fairly sure that I have to access my MasterViewController from here by drilling down through the View Hierarchy, but not sure on how to achieve this effectively?
On a side note, the segue does work, I am presented with the correct views, but it pushes the view onto the screen twice and I'm not sure why, I can upload a gif of this if seeing what I am talking about in action might make more sense?
You can "reach down" through the hierarchy of view controllers, to get a reference to the table view controller titled "Sections" in the storyboard.
From prepareForSegue in your SurveyViewController:
// the destination of the "embed" segue will be the split view controller
let splitVC : UISplitViewController = segue.destinationViewController as! UISplitViewController
// in a UISplitViewController, viewControllers[0] represents the Master view controller
// in your case that is a UINavigationController...
let navVC: UINavigationController = splitVC.viewControllers[0] as! UINavigationController
// In a UINavigationController, topViewController represents the visible VC
// In your case that's the Sections table view controller...
let sectionsVC : SectionsViewController = navVC.topViewController as! SectionsViewController
// (replace "SectionsViewController" with the correct class name)
sectionsVC.object = yourNSManagedObject
This passes the object to the Sections view controller. You can pass the object to the final VCs (the bad boys!) in the prepareForSegue of the Sections view controller. You can't do it earlier, because they are not instantiated before then.
As to why the view might be pushed onto screen twice, my only guess is that you might be using performSegueWithIdentifier from within didSelectRowAtIndexPath of a table view, when the segue is also linked directly to the prototype cell.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
println("Sender is \(sender)")
if segue.identifier == SurveyIdentifier {
// let nav : UINavigationController = segue.destinationViewController as! UINavigationController
// let itemVC: BACUploaderViewController = nav.topViewController as! BACUploaderViewController
let surveyViewController: SurveyViewController = segue.destinationViewController as! SurveyViewController
***********************
surveyViewController.managedObject = self.managedObjectContext
************************
// if let destination = segue.destinationViewController as? MasterViewController {
//
// destination.workItem = sender as? Work
// }
}
}
You can have an optional named managedObject in your SurveyViewController and then inject your managedObjectContext object from MasterViewController to SurveyViewController. This code snippet is written keeping in mind that you are in MasterViewController

No back button on segue in Swift 2

I just ported my project over to Swift 2, and everything is working great - except that even the most simple segues have no back button. Here is the prepare for segue function that I am using:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if segue.identifier == "showExercise" {
if let nav = segue.destinationViewController as? UINavigationController {
if let exercisesController = nav.topViewController as? ExercisesController {
let cell = sender as! WorkoutCell
if let workout = cell.name!.text {
exercisesController.exercises = Workouts[workout]!
exercisesController.navigationItem.title = workout
}
}
}
}
}
Before, the back button to the parent segue used to automatically populate. Now, all I get is the title in the child navigation vc
Are you using show or show detail segue? It seems like you are using a modal segue. Destination view controller for show or show segue is usually the second view controller itself, and not embedded in another UINavigationController.
If your destination view controller for the show segue is really a UINavigationController, the new navigation controller's navigation bar settings may override the old one (the navigation controller of the source view controller). Try not embedding your destination view controller in another UINavigationController.

Resources