UITextFieldDelegate SecondView

发布于 2024-12-17 13:42:08 字数 1222 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妥活 2024-12-24 13:42:08

问题解决了...:)
问题在于从 firstViewsecondView 的连接。
如果您想添加控制器,请不要使用addSubView
使用presentModalViewController :)
希望对您有帮助,以防您遇到像我一样的问题。

Problem solved... :)
The problem was the connection from firstView to secondView.
Do not use addSubView, if you want to add Controller!
Use presentModalViewController :)
Hope it helps, in case you have the same problem like me.

梦里寻她 2024-12-24 13:42:08

在 nib 文件中,请检查您是否选中了文本字段的自动启用回车键复选框。

In the nib file, please check that whether you have checked the Auto-enable Return Key check box for the text field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文