Josh
Josh

Reputation: 263

How does self.textField.delegate = self work in swift?

I am working with keyboard resign features in iPhone app development. I would like to know why

self.textField.delegate = self 

needs to be included into the viewDidLoad of a viewController. I have tried to find reasons of this but no explanation has been clear so far.

Upvotes: 5

Views: 5812

Answers (5)

mcfedr
mcfedr

Reputation: 7975

A few points

  1. The reason you need to set the delegate is because without it the view doesn't know about the view controller. So it wouldn't know about your method textFieldDidEndEditing and it would never be called.

  2. That is the basic premise of delegate, you are telling that object, "here is an object that I want you to call methods on"

  3. It doesn't have to be set in viewDidLoad - but it's often the most convient place to set up delegates for views.

  4. The delegate doesn't have to be the view controller (self), in your case it's the simplest way, but with a UITableView its common to have another class be the delegate so that all the logic isn't in one place and so it can be changed.

Upvotes: 6

Bhavin Bhadani
Bhavin Bhadani

Reputation: 22374

The UITextFieldDelegate protocol defines methods that you use to manage the editing and validation of text in a UITextField object. All of the methods of this protocol are optional.

A text field calls the methods of its delegate in response to important changes. You use these methods to validate text that was typed by the user, to respond to specific interactions with the keyboard, and to control the overall editing process. Editing begins shortly before the text field becomes the first responder and displays the keyboard (or its assigned input view).

From more info. check apple doc.

Its not necessary to use self.textField.delegate = self if you don't want to manage the editing and validation of text in a UITextField object as all the methods of UITextFieldDelegate is optional.

For your other questions like what does .delegate = self do??

When you "set the delegate," what you are doing is saying where you want the messages to go.

Hence,

blah.delegate = amazingPlace will send the messages to "amazingPlace".

blah.delegate = somewhereElse will send the messages to "somewhereElse".

blah.delegate = self will send the messages to you.

... check this source link for details

Upvotes: 5

Jan ATAC
Jan ATAC

Reputation: 1262

As stated before, UITextfield delegation allows you to control events on your textfield. You ll have the ability to edit functions like

textFieldShoulEndEditing

or

textFieldDidEndEditing

in order to add custom rules, for example : text validation.

Take a look at Apple doc.

If you don't need it, you can delete this line and UITextfieldDelegate on your class declaration.

Upvotes: 0

Gordonium
Gordonium

Reputation: 3487

Delegates are key concepts in iOS development so I'd suggest taking a good look at the documentation for them. It can be particularly useful to create your own custom delegates in certain situations too so understanding and using them in the right places can really help improve the structure of your projects.

There are a couple of key reasons for using them. Firstly, they allow safe communication between classes. In your example, the textField object that you're using is communicating back to your view controller. This is why you need to set your view controller as its delegate. Otherwise the text field doesn't have a delegate object (your view controller) to communicate with. The text field fires certain methods at certain times, such as textFieldDidBeginEditing, and calls these on its delegate object if it has one. When you register your view controller as the text view's delegate you can tap into these callbacks.

The other benefit is that delegates allow you to separate concerns and encapsulate or abstract responsibilities. It might be that the main concern for the text view is how to handle text in its view but not necessarily what to do when has been entered, or when the return button in the keyboard is pressed, or how to validate text that has been input. It's better that these tasks are handed over to something else, such as a delegate (in Obj-C parlance), and that is why in your example you have to register one class as the delegate for another.

Upvotes: 1

Rool Paap
Rool Paap

Reputation: 1976

You need to either set the delegate of a UITextField in code with self.textField.delegate = self or make your viewcontroller (or any other class) a delegate with class MyViewController: UITextFieldDelegate and set the delegate of the UITextField in the storyboard by control dragging from the textfield to the viewController.

UITextField Delegate

Upvotes: -2

Related Questions