I'd like to pass my array from my first to my second ViewController.
In the end of my first VC I tried to pass it like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondVC = ViewController2()
secondVC.passengers2 = passengers
print(secondVC.passengers2)
print(passengers)
}
"passengers" is my first array in "ViewController.swift" and "passengers2" my sewcond array in "ViewController2.swift".
When I go over to my second VC, the console tells me, that "passengers" and "passengers2" have the same value, but as soon as I am in "ViewController2.swift", "passengers2" is emtpy for some reason.
Does anyone know why?
The problem in your code is that you instantiate a new ViewController2 object, which is never used.
You want to use the destination view controller that is inside your segue object, like that :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destinationViewController as? ViewController2 {
destinationVC.passengers2 = passengers
}
}
Related
In Xcode 13.1 I programmed the following code into my source view controller. It's working so well that it's calling the destination view controller twice (instead of once).
This is my code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToNextViewControllerSegue" {
let destVC = segue.destination as! DestinationViewController
destVC.categoryTag = tag
}
}
Any idea what I'm doing wrong?
Let me include the IBAction code as well (This doesn't seem to be the problem, though.)
#IBAction func startButtonTapped(_ sender: UIButton) {
if tag == 1 || tag == 2 {
self.performSegue(withIdentifier: "goToNextViewControllerSegue", sender: nil)
} else {
createAlert()
}
}
Thanks for any help!
Add a breakpoint to prepare(for segue: sender:) and look at the backtrace to see where it is being called from. My guess is that you have connected the button to the segue in the storyboard, and then also to the action method mentioned in your question, so the segue is being performed twice.
I am woking on an recording APP.
I tried to add navigation controller in my first recording viewcontroller which then could pass filename array to the second viewcontroller using the following function prepare:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let controller = segue.destination as? MainViewController
controller?.recordArray = recordingArray
self.present(controller!, animated: true, completion: nil)
}
However, when ran in the simulator the navigation bar disappeared in the second controller and Xcode pops out the warning
Thread1: EXC_BAD_ACCESS(code=2,address=0x7fff51edfff8)
Has anyone got any advice?
Thanks!
Don't ever call present(_:animated:completion:) inside prepare(for segue:).
prepare(for segue) is called automatically by the system just before a segue is about to happen to let you prepare the data for sending to the destination viewcontroller or do any other calculations you need to before performing the segue. A segue needs to be set up in Storyboard and it will either be called automatically or if it is a manual segue, you need to call it using perform(segue) and once you do that, the system will call prepare(for segue) for you. You can see why calling another navigation function proves to be problematic, since you are trying to navigate to another viewcontroller using two different methods (segue and present).
If you haven't set up the segue in Storyboard, then you also need to do that, since if it is not set up, let controller = segue.destination as? MainViewController will be nil.
Once you set up the segue in Storyboard, this is how your function should look like:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let controller = segue.destination as? MainViewController {
controller.recordArray = recordingArray
self.present(controller, animated: true, completion: nil)
}
}
You shouldn’t be trying to present a VC in this method, it’s just a place for you to configure the destination VC before the segue presents it
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let controller = segue.destination as? MainViewController else { return }
controller.recordArray = recordingArray
}
I have a source view controller and a destination view controller.
I would like to show data fetched from a url in the UI (destination) just after moving to this screen ("show" segue).
In order to do that, in the source VC I use "prepare for segue" method, where I call a function that returns an array with all the fetched data I want to show and pass it to the destination VC to be shown in a UITableView.
The problem is that many times the whole data is not fetched from the url, so I pass an empty array to the destination.
This is the code in the source VC:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVc = segue.destination as?
ShowCharacterInfoViewController{
fetchCharacterArrayFromUrl(characterName: cellContent){ results in
destinationVc.array.append(contentsOf:results)
destinationVc.tabelView.reloadData()}
} }
I can't think of a proper solution.
You can do one of two things.
Load the data and only perform the segue after the function has returned the data.
Transition to the destination screen and have that controller load the data in viewWillAppear.
// Add a place to save the results
var saveResults: MyResultsType?
#IBAction func someButtonAction(_ sender: Any) {
// Consider putting a "Loading..." dialog here
fetchCharacterArrayFromUrl(characterName: cellContent){ results in
self.saveResults = results
DispatchQueue.main.async({
// Hide the "Loading..." dialog
performSegue(withIdentifier: "ID of your Segue", sender: sender) //
})
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVc = segue.destination as? ShowCharacterInfoViewController {
// I'm not sure append() is really what you need here
destinationVc.array.append(contentsOf: savedResults)
destinationVc.tabelView.reloadData()
}
}
You need to update the UI stuff in main thread since you are executing backend call:-
DispatchQueue.main.async({
destinationVc.tabelView.reloadData()
})
I have 1 UIViewController that contains a UIContainerView and a UIButton.
Also, I have a UITableViewController (It has a UITextField and a UITextView) that is embedded in the UIContainerView in the UIViewController.
I'm trying to get the string values that will be available in TextField and in TextView.
I tried to use the segue to get the properties values, but I failed to do so, see the code below.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "TVCSegue" {
let containerTVC = segue.destination as? FieldsTVC
self.textField.text = FieldsTVC?.textField?.text
self.textView.text = FieldsTVC?.textView?.text
}
}
The code above has 'textField' and 'textView' as properties in the UIViewController to assign the values to.
But I believe that it doesn't work as I get the values before they changes. Please provide me with a functional way to do so.
When your main view loads, the container performs a segue to its assigned initial / root ViewController. At that point, you can get a reference to it:
var theFieldsTVC: FieldsTVC?
Now, in prepare for segue, assign that variable:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "TVCSegue" {
if let vc = segue.destination as? FieldsTVC {
theFieldsTVC = vc
}
}
}
Then, you can:
#IBAction func buttonTapped(_ sender: Any) {
self.textField.text = theFieldsTVC?.textField?.text
self.textView.text = theFieldsTVC?.textView?.text
}
I am trying to send data via prepareforsegue to a TabBarController that is embedded in a NavigationBarController.
This was my last attempt and it did not work
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var destinationViewController = tabBarController!.viewControllers![0] as! ProgressStartViewController // or whatever tab index you're trying to access
destinationViewController.departureAirportLineChart = "data1"
destinationViewController.arrivalAirportLineChart = "data2"
}
Any ideas?