iOS Swift Basics Tutorial: Protocols and Delegates
Why Stack Overflow recommends it
Stack Overflow users link it in answers about passing data between view controllers, especially sending data back to a previous view controller with a delegate.
Recommended by 2 people across 3 different months, 2015 to 2016.
What it is
A 13-minute iOS and Swift tutorial by Daniel Autenrieth that explains the delegation design pattern using protocols and delegates. It walks through the common case of sending data from one view controller back to another.
What people said
Recommends Suragch · 2015-08 · Stack Overflow · on “Passing data between view controllers”
Swift There are tons and tons of explanations here and around Stack Overflow, but if you are a beginner just trying to get something basic to work, try watching this YouTube tutorial (It's what helped me to finally understand how to do it). YouTube tutorial: How to send data through segue (Swift) [LINK] Passing data forward to the next View Controller The following is an example based on the video. The idea is to pass a string from the text field in the First View Controller to the label in the Second View Controller. [LINK] Create the storyboard layout in the Interface Builder. To make the segue, you just Control click on the button and drag over to the Second View Controller. First View Co
Recommends Suragch · 2015-10 · Stack Overflow · on “Pass data back to previous viewcontroller”
Swift: Sending data back using the delegate pattern My full answer that covers passing data both ways is here [LINK] . My answer explaining the delegate pattern is here [LINK] . To pass data back from the second view controller to the first view controller, you use a protocol and a delegate. This video is a very clear walk though of that process: YouTube tutorial: iOS Swift Basics Tutorial: Protocols and Delegates [this video] . But also read this post [LINK] to make sure you don't get into a strong reference cycle. The following is an example based on the video (with a few modifications). [LINK] Create the storyboard layout in the Interface Builder. Again, to make the segue, you just Control dra
Recommends MikeG · 2016-01 · Stack Overflow · on “Passing data between two View Controllers which aren't connected by a segue”
You can set up a delegate pattern in order to do this. Here are the steps for setting up the delegate pattern between two objects, where object A is the delegate for object B, and object B will send messages back to A. The steps are: Define a delegate protocol for object B. Give object B an optional delegate variable. This variable should be weak. Make object B send messages to its delegate when something interesting happens, such as when it needs a piece of information. You write delegate?.methodName(self, . . .) Make object A conform to the delegate protocol. It should put the name of the protocol in its class line and implement the methods from the protocol. Tell object B that object A is
Cites Suragch · 2015-09 · Stack Overflow · on “How to send message to grandparent View Controller in Swift”
I understand how to send data to a child View Controller [LINK] when calling it with a segue. I think I also understand how to send data to a parent View Controller [this video] through the use of protocols and delegates. But how do I send data to a grandparent View Controller? Options that I've considered: Send the data to the parent and have the parent send it to the grandparent. But this seems like a lot of extra work when then parent doesn't need to know. Use NSNotificationCenter . But (1) it is only the grandparent that needs to know, not the whole world, and (2) from the scant Swift documentation that I've found, NSNotificationCenter seems very unSwiftlike.
Cites mfaani · 2016-09 · Stack Overflow · on “Passing data between view controllers”
I watched the video over and over. Still very confused about 'which class is delegating to which class'. To my understanding (similar to a tableView that doesn't know its number of sections) viewController doesn't know what it's label's text should be. So it delegates it secondViewController. So **inside** secondviewController we should have `viewcontroller.delegate = self`. But in this moment [this video] of the video it's exactly the opposite. Where am I getting it wrong?