Passing a variable between ViewControllers in Xcode [duplicate] - ios

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 2 years ago.
I'm trying to pass a basic string array between two ViewControllers, each of which controls a different tab view in an iOS Xcode project. I collect the data from a JSON file in one of the controllers, and I want to pass various string arrays I'll collect from that data to a different tab, or the other ViewController. How would I go about doing this?
I'm fairly new to Swift and Xcode, so apologies if this is a stupid question. Any help is appreciated!

You can use the prepare(for segue: UIStoryboardSegue, sender: Any?) function if you are using storyboards:
Simply override this function in your first UIViewConteroller (i.e. the instance that holds the data that you want to pass to the second UIViewController):
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if var viewController = segue.destination as? MySecondViewController
{
// Set data
// viewController.stringArray = self.stringArray
}
}

Related

Passing Data Between two ViewControllers in MVVM

I recently started exploring MVVM and stuck at passing data between 2 view controllers.I have an array of items in my viewModel1 of ViewController1 and I want to pass that array to viewcontroller2. I am able to achieve it by getting the whole array from viewModel1 like below but I am not sure if this is the right way to do it.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == Constants.kShowDetailSegue {
let destinationViewController = segue.destination as! ListViewController
destinationViewController.products = viewModel1.getFavouriteProducts()
//the getFavouriteProducts gets data from coredata.
}
}
I think you have done it the right way, but alternatively you can use closures. Which are the preferred, method of communication within iOS. You will take care of memory leaks wherever necessary by using
weak
OR
unowned

Swift 3: only nil values in destination view controller of segue [duplicate]

This question already has answers here:
Changing label text of second ViewController upon clicking button in first ViewController
(4 answers)
Closed 6 years ago.
When using the prepare (forSegue) method to pass data to the destination view controller of a segue, all members of the destination VC are nil. Thus, my program crashes when trying to access these members.
My code in the source view controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Second2DetailEdit"{
var detailVC = segue.destination as! DrinkDetailViewController
detailVC.headerLabel.text = "Edit Drink"
}
//...
}
The VC itself is not nil. Only all members of the VC. Did I forget something?
This does only occur in my new Swift 3 project. Similar code in my Swift 2 projects have no problems.
Thanks in advance.
The destination view controller's view is not created yet and that's why all IBOutlets are nil. Try sending the information you need in normal properties like Strings and in ViewDidLoad of the destination, update your view.
The best way around this is to create a variable in your second view controller and set the text to that instead.
Then once the vc has initialized you can set the headerLabel.text to the new variable in viewDidLoad
I would suggest safely unwrapping your detailVC rather than force unwrapping...
if segue.identifier == "Second2DetailEdit" {
if let detailVC = segue.destination as? DrinkDetailViewController {
detailVC.headerLabel.text = "Edit Drink"
}
}
You'll have an easier time setting properties on the destinationVC and passing info to those rather than trying to do it through UILabels...

prepare(for segue: UIStoryboardSegue, sender: Any?) Not firing in Xcode 8.1 swift 3 ios 10

I have a tabbed view controller that is associated with two view controllers.
I need to pass an array from the first View Controller to the Second View controller.
In Order to do this I have the following code in my first VC:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "AddItem"
{
if let destinationVC = segue.destination as? SecondViewController
{
destinationVC.toDoList = toDoList
}
}
}
However this is not getting fired when I switch two the second VC using the tab button.
Any ideas as to why this is not getting fired?
This is how my main storyboard looks like:
Main Storyboard
You cannot transfer objects between tab views through segue because the VC is not actually making a direct segue connection rather you can do it with a delegate or a notificationCenter post and observe to transfer
You have to put that segue code in Tabbar controller class. You shouldn't put that thing in firstVC. Because there is no segue is going on from first VC to Second VC.

Pass data between multiple view controllers in swift [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 6 years ago.
I'm a beginner in programming, so got a question. I have an app with 3 viewControllers. First 2 has inputs. So, the question is, how can I pass data from 1st and 2nd VC to the third one? Thanks a lot for help.
Most use segues between each view controller typically (VC#1 >> VC2 >> VC3). If you are doing that, you simply need to declare public properties along the route and pass them.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Segue VC #2" {
if let vc = segue.destination as? SecondViewController {
vc.property1 = self.propertyToSend
}
}
}

iOS Swift - Presenting View Controllers and passing info between them [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 6 years ago.
I'm studying for the iOS Developer Nanodegree from Udacity and I can't figure out how to pass information between view controllers.
In this video, the instructor ask that we present view controllers using code, segue & code, and segue.
https://www.youtube.com/watch?v=zGzu5PcP8TI
I spent hours trying to understand this but I'm not getting anywhere. Is there any resources that clearly explains this?
Let's say you have VC1 and VC2 (two UIViewController classes) and that they are both in the same Storyboard file.
VC1 and VC2 both have a property:
var aProperty: String
VC1 has a UITextField, and when it changes, you set aProperty to the text field's text.
Now, you want to show VC2 and VC2 needs the value of aProperty from VC1.
If you set up a segue from VC1 to VC2, when it is triggered, this function in VC1 will be called (if it exists)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// You can set up VC2 here
}
For example:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let vc2 = sender as? VC2 {
vc2.aProperty = vc1.aProperty
}
}

Resources