When implementing the UITextFieldDelegate in my ViewController class, the following error is thrown when entering the first character in the text field:
-[MyViewController respondsToSelector:]: message sent to deallocated instance...
So, I tried creating a separate class (inheriting only NSObject) and implementing UITextFieldDelegate. Guess what, it worked perfectly. However, that introduces some other problems as I have to do a lot of ugly cross-class-communication that I'd like to avoid. Here's the relevant parts of my app delegate code:
@interface RMSAppDelegate : NSObject <UIApplicationDelegate,
UITabBarControllerDelegate>
@property (nonatomic, retain) UIViewController* myViewController;
@end
@implementation MyAppDelegate
@synthesize myViewController;
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
myViewController = [[MyViewController alloc]
initWithNibName:@"MyView" bundle:nil];
[self.window setRootViewController:myViewController];
[self.window makeKeyAndVisible];
return YES;
}
@end
.. and here's what is being displayed:
@interface MyViewController : UIViewController <UITextFieldDelegate>
@property (nonatomic, retain) IBOutlet UITextField* pinTextField;
- (void)viewDidLoad;
@end
@implementation MyViewController
@synthesize pinTextField;
- (void)viewDidLoad {
// DOES NOT WORK (WHY?)
//[pinTextField setDelegate:self];
// WORKS, BUT I'D LIKE TO AVOID
[pinTextField setDelegate:[[[MyTextFieldDelegate alloc] init] autorelease];
[pinTextField becomeFirstResponder];
[super viewDidLoad];
}
@end
And please, if you see any code (even off topic) that I could be doing better, leave a comment.