我正在使用iOS 5,并试图在UITextFieldDelegate中使用TextField,它的工作方式与我想要的完全一样(但就在第一个视图中)。我不明白,为什么它不能在下一个视图中工作。
例如,我创建了新项目(ViewController)。在那里,我添加了一个按钮,它连接到另一个视图(SecondViewController)。在SecondViewController中,我有一个TextField。对于这个textField,我想使用textFieldShouldReturn。但是,这个方法似乎没有被调用。据我所知,我应该用ViewDidLoad写代表。我应该写myTextField.delegate =self吗?但我觉得有些地方不对劲。我用了调试,总是在那个位置,我遇到了问题。你能告诉我,有什么问题吗?我如何解决这个问题?
提前谢谢.
下面是我的代码(它在第一个视图(ViewController)中工作)。不幸的是,这里没有(SecondViewController):
SecondViewController.h
@interface SecondViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *myTextField;
@endSecondViewController.m
@implementation SecondViewController
@synthesize myTextField;
- (void)viewDidLoad{
[super viewDidLoad];
myTextField.delegate = self; // here i get the problem
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{ // this method is not being called
[textField resignFirstResponder];
NSLog(@"is called!");
NSString *valueMyTextField = myTextField.text;
NSLog(@"%@", valueMyTextField);
return YES;
}发布于 2011-11-22 12:30:35
问题解决了..。)问题是从firstView到secondView的连接。如果要添加控制器,请不要使用addSubView!使用presentModalViewController :)希望它有帮助,以防你和我有同样的问题。
发布于 2011-11-21 10:21:55
在nib文件中,请检查是否选中了文本字段的自动启用返回键复选框。
https://stackoverflow.com/questions/8210083
复制相似问题