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

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
}
}

Related

How to copy design from destination in UIStoryboardSegue animation? [duplicate]

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.

Do i have to use delegate when I want to change something from on VC in another VC ? SWIFT [duplicate]

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!
}

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
}
}
}

How to identify the kind of StoryboardSegue

In my iOS project I use two kinds of UIStoryboardSegue, which present a view either within a navigation controller or as a modal view. I set the kind property in Interface Builder to:
Show (e.g. Push)
Present Modally
Now I want to be able to programmatically identify the kind of segue in order to customise the appearance of my ViewController. Like so:
class ViewController : UIViewController {
var isModal : Bool = false
...
}
class OtherViewController : ViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.destinationViewController is ViewController {
let vc = segue.destinationViewController as! ViewController
vc.isModal = TODO
}
}
}
I was hoping there would be a property, but I can't find it. I was also hoping that the segue class would differ, but I also can't find enough documentation.
I originally stumbled upon this problem trying to use the isModal in order to alternate between dismissing the ViewController vs. popping the ViewController. I have noticed that there now seems to be a better alternative, which is the UnwindSegue. However, I still need the flag in order to customise appearance..
Thanks
Maybe I'm totally wrong but can't you use the identifier of the segue?
For example name all modal view controllers with Modal<Name>. Then check
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier.hasPrefix("Modal") {
let vc = segue.destinationViewController as! ViewController
vc.isModal = TODO
}
}

Resources