Seems to be undocumented changes in how UITextFieldDelegate protocol works between iOS 4.3 and iOS 5.0 which are breaking my code.
Simple to reproduce as follows: Using XCode 4.2, create a 'Single View Application' project called test. Use default options (i.e. no Storyboard, but yes to ARC) Set deployment target to 4.3 (so we can run the app in both 5.0 and 4.3)
Put this code in the ViewController.h header file:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
@property(nonatomic,strong) UITextField *textField1;
@property(nonatomic,strong) UITextField *textField2;
@property(nonatomic,strong) UILabel *resultsLabel;
@end
Put this in the ViewController.m file:
#import "ViewController.h"
@implementation ViewController
@synthesize textField1,textField2,resultsLabel;
- (void)viewDidLoad {
textField1=[[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 30)];
textField1.tag=1;
textField1.backgroundColor=[UIColor whiteColor];
textField1.delegate=self;
[self.view addSubview:textField1];
textField2=[[UITextField alloc] initWithFrame:CGRectMake(10, 50, 300, 30)];
textField2.tag=2;
textField2.backgroundColor=[UIColor whiteColor];
textField2.delegate=self;
[self.view addSubview:textField2];
resultsLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 90, 300, 30)];
resultsLabel.backgroundColor=[UIColor clearColor];
[self.view addSubview:resultsLabel];
[super viewDidLoad];
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
switch (textField.tag) {
case 1:
// textField1
[textField2 becomeFirstResponder];
resultsLabel.text=@"Finished editing 1st box";
break;
case 2:
// textField2
resultsLabel.text=@"Finished editing 2nd box";
break;
}
}
@end
Run the code in the simulator and it displays two white UITextFields. Tap the first one, then tap the second one.
If run in iOS 4.3 on the simulator the UILabel displays "Finished editing 1st box" If run in iOS 5.0 on the simulator the UILabel displays "Finished editing 2nd box"
I am trying to make it so that when you leave field i you automatically go to field (i+1). Any suggestions as to what's wrong, and how to fix it? I can't see any mention in Apple's iOS 5 release notes.
By touching the second box its automatically becoming the first responder so setting it progmatically is setting it twice.
-(void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == textField2) {
resultsLabel.text=@"Finished editing 2nd box";
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField == textField1) {
[textField1 resignFirstResponder];
[textField2 becomeFirstResponder];
}
if (textField == textField2) {
[textField2 resignFirstResponder];
[textField1 becomeFirstResponder];
}
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField == textField2) {
resultsLabel.text=@"Finished editing 1st box";
}
}
This will work and it will automatically switch first responder by hitting return.