似乎是无证如何UITextFieldDelegate协议的iOS 4.3和iOS 5.0被打破我的代码之间的工作变动。UITextFieldDelegate协议在iOS 4.3和5.0之间更改。发生了什么?
简单重现如下: 使用的XCode 4.2,创建一个“单一视图的应用”项目称为测试。使用默认选项(即没有故事板,但肯定的ARC) 设置部署目标至4.3(所以我们可以运行在两个5.0和4.3的应用程序)
将这个代码在ViewController.h头文件:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
@property(nonatomic,strong) UITextField *textField1;
@property(nonatomic,strong) UITextField *textField2;
@property(nonatomic,strong) UILabel *resultsLabel;
@end
在ViewController.m文件,将这个:
#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];
[email protected]"Finished editing 1st box";
break;
case 2:
// textField2
[email protected]"Finished editing 2nd box";
break;
}
}
@end
运行在模拟器上的代码,并显示两个白色UITextFields。点击第一个,然后点击第二个。
如果在模拟器中的UILabel显示“编辑完第一个盒子” 运行的iOS 4.3。如果在安装iOS 5.0模拟器上运行的UILabel显示“编辑完成第2盒”
我试图让它如此当你离开现场时,你会自动去场(i + 1)。有什么不对的建议,以及如何解决它?我在Apple iOS 5发行说明中看不到任何提及。
这种方法有一个问题:你想如何解散键盘? – 2012-01-05 02:57:10
非常感谢(接受为正确答案:-)我正在编写的实际代码也会响应textFieldShouldReturn,因此如果您在field1上点击'enter',您将自动转到field2 - 因此需要以编程方式将其设置在某个点上。奇怪的是,iOS4.3和5.0以不同的方式做到这一点 - 我猜想某种无证的功能。就好像4.3会从textField中放弃firstResponder,而5.0只是将firstResponder从无论什么情况下辞职.....再次感谢。 – Peter 2012-01-05 12:36:16