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.

其他提示

I suspect that textFieldDidEndEditing: is being called twice.

Once when you select textField2 (which ends the editing in textField1) and then again when you call [textField2 becomeFirstResponder];: Since textField2 it is already the firstResponder (because you touched it in order to end the editing on textField1) when you tell it to become the firstResponder again, it actually resigns it's firstResponder status (which invokes textFieldDidEndEditing) and then becomes the firstResponder again.

Try this changing [textField2 becomeFirstResponder]; to this:

 if (![textField2 isFirstResponder]) {
      [textField2 becomeFirstResponder];
 }  
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top