Swift Delegate Protocol Pattern Tutorial - iOS Communication Patterns Part 1

Sean Allen · 169K views · 2017 · 13:06

Open on YouTube

Software practice, open source & careersSoftware architecture & designProgramming language designtutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering how to pass data or trigger methods between view controllers, since it gives a short, clear demonstration of the delegate and protocol setup.

Recommended by 8 people across 7 different months, 2017 to 2022.

What it is

A 13-minute Xcode walkthrough by Sean Allen of the delegate protocol pattern in Swift, used for communication between view controllers and other classes. It is part 1 of his iOS interview questions series and builds a working example from scratch.

What people said

Recommends Sean · 2017-10 · Stack Overflow · on “How to reference UITableViewController from a UITableViewCell class?”

Just got to say thank you so much. Sorted me out. If anyone would like more info on Delegates and Protocols watch this great short video: [this video]

Recommends Axemasta · 2018-06 · Stack Overflow · on “I have a UISwitch on the secondView, how do I control the switch method”

Delegates are extremely useful, this is a good video going through setting them up [this video] You can use them for web calls etc, they are essential for patterns like facade.

Recommends Gabriel · 2018-10 · Stack Overflow · on “How to access private variable in swift child class from objective-c superclass?”

First solution Are you sure that you can not edit Pod script ? May be it's lock and the only thing you have to do is unlock the script and switch private variables to public (or delete the 'private' because in swift the variables are automatically public) Second solution You can use protocol to access to this variables I found for you a perfect tutorial Protocol/Delegate [this video] Third solution USE IT ONLY IF YOU HAVE NO IDEA: Use global variable, but it's not recommended to use this method. So if nobody here give you a better idea and you are stuck on this problem, use them until you find a better solution

Recommends Mohammed Zaki · 2019-09 · Stack Overflow · on “How to set Struct Variable data from one view controller And Getting same value from Another view controller”

I would rather use Protocols and delegators. Here is a great tutorial will help you understand the concept Sean Allen Swift Delegate Protocol Pattern Tutorial - iOS Communication Patterns Part 1 [this video]

Recommends Maksim Chizhavko · 2020-05 · Stack Overflow · on “UI Update Delegate Method Not Called”

The problem is - you have not connected your delegate with second view where you want to update content For make it works follow next steps: 1) Create Protocol 2) Create delegate variable with your protocol type, you can make it optional 3) Chose where you want to call method from your delegate 4) In related VC assign it itself 5) Subscribe to protocol 6) Add protocol stubs 7) Configure your functionality For better understanding: [this video]

Recommends Gayal Rupasinghe · 2020-06 · Stack Overflow · on “How to invoke a function in another ViewController?”

Google delegate pattern for this. YT: [this video] Hopefully this helps you out. By the way delegate pattern works even if you don’t want to pass data in between.

Recommends kapKr21 · 2020-06 · Stack Overflow · on “Difference between protocol and delegates?”

Delegate and Protocol come under the concept of (one to one) communication pattern b/w viewControllers or classes. This means that one class or viewController is talking to another class or viewController ( Point to remember one to one communication ).m 1st class or viewController gives (delegates) it's reference to the 2nd class it is talking to, by confirming the protocol of 2nd class. Using reference of 1st class, 2nd class does the work on behalf of the 1st class and therefore provides information back to the 1st class. Please watch implementation of the concept using below link : [this video] . Formal explanation is - Protocol is a set of methods (either optional or required) that would be i

Recommends Peter Mattheisen · 2022-01 · Stack Overflow · on “How to control the order of execution between ViewControllers in swift?”

Duncan's advice to add a delegate property is the correct answer which brought me to the solution. I would like to share here the code from the question above enhanced with the delegate mechanism in case it can help somebody with a similar problem. In order to understand the delegate/protocol mechanism, I found a great explanation video from Sean Allen: [this video] Now the code again. Code of main view controller: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() print("Intended step 1 (at main view start)") } @IBAction func gotoSubView(_ sender: UITapGestureRecognizer) { print("Intended step 2 (after tap in main)") let nextViewController = s

Cites vbuzze · 2018-06 · Stack Overflow · on “Swift- Pass textfield from swift file to other swift file”

To pass your information from one view to another you have several options : pass it through the segue (one to one) use protocols & delegates (one to one) use events & observers (one to many) use a third class responsible for holding the current data (one to many) In your case, using Protocols & Deleguates is the proper option to choose. Example provided here [this video] .