当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - ios what does a delegate default to

问题描述:

If you don't specify a delegate, is the self the default delegate?

Also, if you have a UITextField do you need to make sure it's knows it is a UITextFieldDelegate or is it one by default?

网友答案:

If you don't specify a delegate,

Of what object? It is up to the implementation of the specific object to decide.

is the self the default delegate?

No, in the absence of any other information, the delegate would probably be nil.

However, if you were writing a class implementation, and wanted the object to set its delegate to self by default, you could certainly do that. I think this would usually be more confusing than useful, though.

If you want to do something by default, just check if the delegate is nil. (Somebody could always set your delegate to be nil, so you have to do that check anyway.)

If you have a UITextField do you need to make sure it knows it is a UITextFieldDelegate or is it one by default?

UITextField does not implement any of the methods in UITextFieldDelegate -- or at least it doesn't advertise that it does so -- so I'm not sure what you mean here. A UITextField by itself cannot function as a UITextFieldDelegate.

If you were to subclass UITextField and add methods in the UITextFieldDelegate protocol, then you could mark it as implementing UITextFieldDelegate:

@interface MyTextField : UITextField <UITextFieldDelegate>

But that doesn't happen by default, you have to make it happen.

分享给朋友:
您可能感兴趣的文章:
随机阅读: