UITextFieldDelegate SecondView
我正在使用 iOS 5 并尝试将 TextField 与 UITextFieldDelegate 一起使用,它的工作原理与我想要的完全一样(但只是在第一个视图中)。我不明白,为什么它在下一个视图中不起作用。
举个简单的例子,我创建了新项目(ViewController)。我在那里添加了一个按钮,用于连接到另一个视图(SecondViewController)。在 SecondViewController 中,我有一个 TextField。对于这个textField,我想使用textFieldShouldReturn。但似乎这个方法没有被调用。据我所知,我应该在 ViewDidLoad 中编写委托。我应该写 myTextField.delegate = self; ?但我认为那里有问题。我使用了调试,并且总是在那个位置,我遇到问题。你能告诉我,问题是什么吗?我该如何解决它;(
提前致谢......
这是我的代码(它在第一个视图(ViewController)中有效。不幸的是这里不是(SecondViewController):
SecondViewController.h
@interface SecondViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *myTextField;
@end
SecondViewController.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;
}
I'm using iOS 5 and trying to use TextField with UITextFieldDelegate, it's worked exactly like I want (but JUST in the first View). I don't understand, why it's not working in the next view.
For simple example, I created new project (ViewController). There I added one button, that connect to another view (SecondViewController). In the SecondViewController, I have one TextField. With this textField I want to use textFieldShouldReturn. But it seems, that this method is not being called. What I know, I should write the delegate in ViewDidLoad. Should I write myTextField.delegate = self; ? but I think something wrong there. I used Debugging, and always at that position, i'm getting problem. Could you please tell me, what the problem is? and how can i solve it ;(
Thanks in advance.....
Here is my code (that it works, in the first view (ViewController). Unfortunately here not (SecondViewController):
SecondViewController.h
@interface SecondViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *myTextField;
@end
SecondViewController.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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题解决了...:)
问题在于从
firstView
到secondView
的连接。如果您想添加控制器,请不要使用
addSubView
!使用presentModalViewController :)
希望对您有帮助,以防您遇到像我一样的问题。
Problem solved... :)
The problem was the connection from
firstView
tosecondView
.Do not use
addSubView
, if you want to add Controller!Use
presentModalViewController
:)Hope it helps, in case you have the same problem like me.
在 nib 文件中,请检查您是否选中了文本字段的自动启用回车键复选框。
In the nib file, please check that whether you have checked the Auto-enable Return Key check box for the text field.