使用委托以编程方式关闭文本字段键盘

发布于 2025-01-02 01:26:34 字数 883 浏览 3 评论 0原文

我有一个这样的 viewController:

@interface RootViewController : UIViewController{

然后我在 viewController.m 中以编程方式添加了一个文本字段:

UITextField*textFieldRounded=[[UITextField alloc] initWithFrame:frame];
textFieldRounded.delegate = self;

在同一个 .m 中我已经尝试了所有这些:

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
NSLog(@"returned?");
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder]; 
NSLog(@"returned?");
 return YES;
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
NSLog(@"returned?");
    return YES;

}

当我单击文本字段(位于 viewController 的子视图中)时,键盘显示正常。但按“完成”没有任何作用。 NSLog 语句永远不会出现,因此这些方法永远不会被调用。有什么想法吗?

I have a viewController with this:

@interface RootViewController : UIViewController <UITextFieldDelegate>{

Then I have a textfield added programmatically like this in viewController.m:

UITextField*textFieldRounded=[[UITextField alloc] initWithFrame:frame];
textFieldRounded.delegate = self;

In the same .m i have tried all of these:

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
NSLog(@"returned?");
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder]; 
NSLog(@"returned?");
 return YES;
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
NSLog(@"returned?");
    return YES;

}

The keyboard appears fine when I click the text field, which is in a subview of the viewController. but pressing Done does nothing. The NSLog statements never appear, so these methods are never getting called. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

∞梦里开花 2025-01-09 01:26:34

也许我只是几乎没有这个问题,但我所做的是将点击手势识别器对象附加到视图,创建了一个名为“清除键盘:”的实例方法,并在函数内部放置了 [whateverItIsThatOpenedTheKeyboard resignFirstResponder];就是这样。也许你问的问题完全不同,但我想我会尽力提供帮助:)

I just barely had this question, maybe, but what I did was attached a tap gesture recognizer object to the view, made a instance method called clear keyboard:, and inside the function I put [whateverItIsThatOpenedTheKeyboard resignFirstResponder]; and that's it. Maybe you asking something totally different, but I thought I would try and help :)

樱娆 2025-01-09 01:26:34

将 .h 文件中的 UITextField 声明为具有属性的 IBOutlet,并将其合成到 .m 文件中。

Declare the UITextField in .h file as an IBOutlet with property and synthesize it in .m file.

丑疤怪 2025-01-09 01:26:34

使用此委托方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; 

将此委托方法添加到您的代码中并再次运行您的应用程序。

Use this delegate method

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; 

Add this delegate method to your code and run your app once again..

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