I'm creating a simple project for iPhone, using XCode and Interface Builder. While I understand what a delegate is, I have a problem with using it.
I have an UITextField in my interface. It displays keyboard when user taps on it, but I need to program manually how to hide keyboard. It can be done using delegates. So in IB, I'm taking Object from library, giving it's class name as Control1Delegate and then connecting the delegate outlet from my textfield to be this Control1Delegate. I also have .m and .h files for this Control1Delegate class :
Control1Delegate.h
@interface Control1Delegate : NSObject <UITextFieldDelegate> {
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField;
@end
Control1Delegate.m
#import "Control1Delegate.h"
@implementation Control1Delegate
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
@end
But this doesn't work. When running, it just never reaches the textFieldShouldReturn method or crashes without msg or with EXEC_BAD_ACCESS. The funny thing is that when I move the method into controller file (the one wizard has generated) and connect from UITextField to this controller (File's Owner), everything works as expected. I saw that most apple code tutorial puts delegated methods into random objects rather then separate class - I would like to know why. Can't I have delegate in separate class?
What I'm missing here? Some null pointer? Object livecycle?