Reputation: 11
i need to send a variable to another view controller in swift using delegate.
I got 2 view controller : ViewControllerOne and ViewControllerTwo A button in ViewControllerOne linked to ViewControllerTwo using a segue(show) A variable to pass from ViewControllerOne to ViewControllerTwo.
Can someone tell me the code i need to write in each view controller's class to send the variable to ViewControllerTwo ? (I am a beginner, I'll be grateful if you can explain to me how it works)
I know i can use segue to send it, but i need to send it thru delegate.
Thank You
Upvotes: 0
Views: 633
Reputation: 270980
I think you misunderstood when to use delegates to pass data.
It's not like there are 2 methods (by prepareForSegue
or by delegate) you can choose from whenever you want to pass data from 1 VC to another. These two methods should be used appropriately in suitable situations.
Passing data by prepareForSegue
is usually used when data is going from a VC that presents another VC to the VC that is presented. For example, when the user taps on a cell in a table VC, the table VC can pass whatever item the user tapped to the next VC showing details of the item tapped. A prerequisite to pass data using this method is that a segue must connect the data sender and the data receiver.
Passing data by delegate is the other way round. It is usually used when data is being passed from a presented VC to the VC that presented it. For example, You have a VC that shows a UIImagePickerController
. UIImagePickerController
has a delegate that passes which photo/video the user has picked to your VC. This method requires the data receiver to have an instance of the data sender at some point. This is so that the receiver can set itself as the sender's delegate.
Now, let's look at your situation. Using prepareForSegue
to pass data is your best choice, because:
Passing data by delegate here is pretty pointless. I will show you how to do it, then you'll see why.
Here's the delegate:
protocol ViewControllerOneDelegate {
func acceptData(someInt: Int)
}
Now give your VC2 a storyboard ID. Add a property called delegate
in VC1. Make VC2 conform to the delegate:
class ViewControllerOne: UIViewController, ViewControllerTwoDelegate {
// implement acceptData however you like, doesn't matter
}
When you want to present VC2, do this:
let sb = UIStoryboard(name: "Main", bundle: Bundle.main)
let vc2 = sb.instantiateViewController(withIdentifier: "Your Identifier") as! ViewControllerTwo
self.delegate = vc2
Now you're ready to pass the data, on the next line:
self.delegate.acceptData(someInt: 10)
Now do you see something wrong here? These two lines:
self.delegate = vc2
self.delegate.acceptData(someInt: 10)
Why not just get rid of the delegate all together and do:
vc2.acceptData(someInt: 10)
You don't really need to use a delegate here. Just directly assign the data to a variable in VC2 or call a method on VC2 or something. You have the VC2 instance right there! So just pass the data to it!
Upvotes: 2