开发者

What does obj.delegate=self mean?

What does it actually mean to set the delegate of a textfield?

For example: txtFie开发者_运维百科ld.delegate = self


"In short, that you are receiving calls from the txtField. You are setting the object 'self' as the delegate for txtField."

"That means that your 'txtField' will receive events from itself

These two answers essentially mean the same thing. But seemingly contradictory. But the first makes more sense to me. I can see why a beginner gets confused, I've been there!

Basically one is the caller one is the receiver Think of it as a chef in a kitchen call his assistant to cut up some onions. In this particular case, txtField is the chef, "self" is the assistant. txtField orders self "Do this, this and this!" Like it or not the assistant has to oblige cuz he has wife and kids to feed. :)


It means that self will be the recipient of certain method calls that are made in response to actions on the text field.


In short, that you are receiving calls from the txtField. You are setting the object 'self' as the delegate for txtField.


Delegating is a programming pattern that is widely used in Objective-C.

The basic idea is let an object delegate some tasks to another object. For example, your UITextField object delegate some tasks to your view controller. In this case, your UITextField object becomes a delegating object, and the view controller the delegate of the UITextField object. The delegating object sends certain messages to its delegate in order to get necessary information, or to notify certain events, etc.


That means that your 'txtField' will receive events from itself (kind of a weird example, maybe a larger source code section could be provided?)


For some of its methods, the textfield (any object in a class using the delegation pattern) is going to try to call some other object to so that that object can customize some of the textfield's behaviors. The object that the textfield will try call is called it's delegate. The delegate is initially set to nil, so, by default, no customization happens.

If a class has a line of code like: textfield.delegate = self; then it says that this object in this class wants to get called to handle the textfield's customization for certain of the textfield's defined delegate methods.


It means the actual class where 'txtField.delegate =self' is called will receive callsbacks from events. This is often a convenient way to do things.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜