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
}
}
}
Related
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
}
}
This question already has answers here:
Sharing an Image between two viewControllers during a transition animation
(2 answers)
Closed 5 years ago.
Lets say I have two UIViewControllers, we can call them ControllerOne and ControllerTwo.
In my app I have created a custom UIStoryboardSegue animation between ControllerOne and ControllerTwo. The effect I want to create with the animation is that it will feel like a part from ControllerTwo sliding onto ControllerOne so that the segue between the two UIViewControllers does not seems to be a segue.
From this point I don´t know which approach is the best one to take. The design for ControllerTwo is set through the storyboard but how do I get it?
(What I know is that I can´t copy the values from ControllerTwo into the segue because they have not been set yet so it will throw an error.)
Usually we Passing Data via this func prepareForSegue in the UIViewController class
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "idFirstSegue" {
let secondViewController = segue.destinationViewController as SecondViewController
//secondViewController.message = ...
}
}
You can also override perform func in the subclass of the UIStoryboardSegue class,
here is the code :
override func perform() {
var firstVC = self.sourceViewController as UIViewController!
var secondVC = self.destinationViewController as UIViewController!
//Then copy design from destination
}
The article fits you , I think.
This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 5 years ago.
I am doing some excercises now. For example I create two view controllers in storybard and I want to change color for example from VC2 in VC1 by clicking button. In this situation delegate is needed or is other way to do it?
Normally, yes. But that depends . You can use NotificationCenter, you can use "prepare" function. The delegate is the most common option here. In your case:
protocol ChangeButtonColorDelegate {
func changeButtonColor()
}
class VC1 : ChangeButtonColorDelegate... {
func changeButtonColor() {
/* change button color here */
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "goto_vc2") {
(segue.dest as! VC2).delegate = self
}
}
}
class VC2 : ... {
var delegate : ChangeButtonColorDelegate!
}
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...
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
}
}