How are delegate methods used [closed]
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more开发者_JAVA技巧 detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this questionI am trying to use a delegate method in my simple iPhone application but I got an error. If there are any examples code; pls send me a link or code.
When using delegates there are a few things to take care of.
For example, say you have a class MyController
and you want it to be a UITextField
's delegate. Put the delegate protocol name in the class's interface:
@interface MyController : NSObject <UITextFieldDelegate>
Then, implement the delegate methods in the UITextFieldDelegate
docs (the docs say "All of the methods of this protocol are optional." so you can choose which you want):
@implementation MyController
...
- (BOOL)textFieldShouldReturn:(UITextField *)textField
// do something
}
...
@end
Then finally, set yourself as the delegate:
myTextField.delegate = self;
精彩评论