Press ESC to close

How Delegates Works in Swift

Hello friends. In this article, we will talk about how Delegates work with Swift. First of all, we should talk about what we use Delegates for. For example, if you have a TextField and you want this control to be triggered because a change has been made, you need to do this with delegates.

First, we need to look at the structure of the UITextField. Apple sets the classes properly to increase the readability and reusability of the code. Therefore, it does not actually put all functions in the same class. That’s why it creates a protocol instead of handling functions inside the UITextField class. The event we call Porotocol is actually other languages. Interface coincides with the same event. He named this protocol UITextFieldDelegate. In order to use the functions in this protocol, it creates a UITextFieldDelegate type delegate in UITextField. Functions in this protocol are triggered when the operating system writes a ley to the keyboard or other operations. If we inherit any of these delegates in a View Controller and override the function, this time we will be able to handle it in the View Controller. Of course, before doing this, we have to make the delegate of that control to the View Controller. In this way, it actually triggers the function in the View Controller. The structure is as follows;

 

If you have questions, you can reach by e-mail or comment. Good work.

Leave a Reply

Your email address will not be published. Required fields are marked *