关闭 iPhone 键盘

发布于 2024-07-10 14:27:54 字数 196 浏览 12 评论 0原文

我正在尝试重新创建类似于 safari 中使用的弹出键盘的东西。

alt text

我可以通过在视图和相应按钮上放置一个工具栏来直观地重现它,但是一旦用户触摸完成按钮,我就想不出任何方法来关闭键盘。

I am trying to recreate something similar to the popup keyboard used in safari.

alt text

I am able to visually reproduce it by placeing a toolbar over my view and the appropriate buttons, however i cant figure out any way to dismiss the keyboard once the user has touched the done button.

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

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

发布评论

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

评论(5

沫雨熙 2024-07-17 14:27:54

您需要记住几件事。 开发人员忘记设置的第一个部分是文本字段的委托

如果您使用 Interface Builder,则必须记住,您需要将 textField 的委托设置为文件所有者。

如果您不使用 Interface Builder,请确保将文本字段的委托设置为 self。 我还包括了 returnType。 例如,如果 textField 名为 gameField:

gameField.delegate = self;
gameField.returnKeyType = UIReturnKeyDone;

您还必须为 ViewController 实现 UITextFieldDelegate

@interface YourViewController : UIViewController <UITextFieldDelegate> 

最后,您需要使用 textFieldShouldReturn 方法并调用 [textField resignFirstResponder]

   -(BOOL) textFieldShouldReturn:(UITextField*) textField {
    [textField resignFirstResponder]; 
    return YES;
}

您的所有文本字段都将使用相同的方法,因此您只需进行一次此设置。 只要为textField设置了delegate,就为界面实现了UITextFieldDelegate,添加textFieldShouldReturn方法并调用
resignFirstResponder 你的集合。

There is a couple of things you need to remember. The number #1 part developers forget to set is the delegate of the textField.

If you are using the Interface Builder, you must remember that you need to set the delegate of the textField to the file Owner.

If you are not using Interface Builder then make sure you set the delegate of the textfield to self. I also include the returnType. For Example if the textField was called gameField:

gameField.delegate = self;
gameField.returnKeyType = UIReturnKeyDone;

You must also implement the UITextFieldDelegate for your ViewController.

@interface YourViewController : UIViewController <UITextFieldDelegate> 

Finally you need to use the textFieldShouldReturn method and call [textField resignFirstResponder]

   -(BOOL) textFieldShouldReturn:(UITextField*) textField {
    [textField resignFirstResponder]; 
    return YES;
}

All your textFields will use this same method so you only need to have this setup once. As long as the delegate is set for the textField, the UITextFieldDelegate is implemented for the interface, you add the textFieldShouldReturn method and call the
resignFirstResponder your set.

薄荷港 2024-07-17 14:27:54

您是否尝试过:

[viewReceivingKeys resignFirstResponder];

其中 viewReceivingKeys 是正在接收文本输入的 UIView?

Have you tried:

[viewReceivingKeys resignFirstResponder];

where viewReceivingKeys is the UIView that is receiving the text input?

山田美奈子 2024-07-17 14:27:54

如果您在 Interface Builder 中构建自己的视图,请将视图控制器设置为文本字段的委托,并在视图控制器中从 UITextFieldDelegate 实现 textFieldShouldReturn: 。

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField 
{
    NSLog(@"%@ textFieldShouldReturn", [self class]);
    [theTextField resignFirstResponder];
    // do stuff with the text
    NSLog(@"text = %@", [theTextField text]);
    return YES;
}

UITextFieldDelegate textFieldShouldReturn:在 iphone cocoa 文档中

If your building your own views in Interface Builder, set your view controller to be delegate for the text field and implement textFieldShouldReturn: from UITextFieldDelegate in your views controller.

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField 
{
    NSLog(@"%@ textFieldShouldReturn", [self class]);
    [theTextField resignFirstResponder];
    // do stuff with the text
    NSLog(@"text = %@", [theTextField text]);
    return YES;
}

UITextFieldDelegate textFieldShouldReturn: in the iphone cocoa docs

夜血缘 2024-07-17 14:27:54

如果您正在谈论从 UITextField 而不是 UITextView 关闭键盘。 你的问题还不清楚吗? 如果您确保您的类在接口文件中被标记为 UITextFieldDelegate

@interface MyController: UIViewController <UITextFieldDelegate> {
   UITextField *activeTextField;

   // ...remainder of code not show ...
}

那么您应该实现如下两个委托方法,

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    activeTextField = textField;!    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    activeTextField = nil; 
    [textField resignFirstResponder];
    return YES;
}

但是如果您使用的是 UITextView > 那么事情就有点复杂了。 UITextViewDelegate 协议缺少与 textFieldShouldReturn: 方法等效的方法,大概是因为我们不应该期望 Return 键是用户希望停止编辑文本的信号。多行文本输入对话框(毕竟,用户可能希望通过按 Return 来插入换行符)。

但是,有多种方法可以解决 UITextView 无法使用键盘辞去第一响应者身份的问题。 通常的方法是当 UITextView 显示弹出键盘时在导航栏中放置一个 Done 按钮。 点击后,此按钮会要求文本视图辞去第一响应者的角色,然后关闭键盘。

但是,根据您规划界面的方式,您可能希望当用户点击 UITextView 本身之外时,UITextView 退出。 为此,您需要对 UIView 进行子类化以接受触摸,然后指示文本视图在用户点击视图本身外部时退出。

创建一个新类,

#import <UIKit/UIKit.h>

@interface CustomView : UIView {
   IBOutlet UITextView *textView;
}

@end

然后在实现中实现 touchesEnded:withEvent: 方法并要求 UITextView 辞去第一响应者身份。

#import "CustomView.h"

@implementation CustomView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}

- (void) awakeFromNib {
    self.multipleTouchEnabled = YES;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches began count %d, %@", [touches count], touches);

    [textView resignFirstResponder];
    [self.nextResponder touchesEnded:touches withEvent:event];
}

@end

添加类后,您需要保存所有更改,然后进入 Interface Builder 并单击您的视图。 在实用工具面板中打开身份检查器,并将 nib 文件中的视图类型更改为 CustomView 而不是默认的 UIView 类。 然后在 Connections Inspector 中,将 textView 出口拖动到 UITextView。 执行此操作后,一旦您重建应用程序,活动 UI 元素外部的触摸现在将关闭键盘。 但请注意,如果您要子类化的 UIView 位于其他 UI 元素“后面”,则这些元素将在触摸到达 UIView 层之前拦截它们。 因此,虽然这个解决方案很优雅,但它只能在某些情况下使用。 在许多情况下,您必须诉诸暴力方法,在导航栏中添加完成按钮来关闭键盘。

If you're talking about dismissing the keyboard from a UITextField rather than a UITextView. Your question isn't that clear? If you are then ensure your class is marked as a UITextFieldDelegate in the interface file,

@interface MyController: UIViewController <UITextFieldDelegate> {
   UITextField *activeTextField;

   // ...remainder of code not show ...
}

and then you should implement the two delegate methods as below,

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    activeTextField = textField;!    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    activeTextField = nil; 
    [textField resignFirstResponder];
    return YES;
}

However if you're using a UITextView then things are a bit more complicated. The UITextViewDelegate protocol lacks the equivalent to the textFieldShouldReturn: method, presumably since we shouldn’t expect the Return key to be a signal that the user wishes to stop editing the text in a multi-line text entry dialog (after all, the user may want to insert line breaks by pressing Return).

However, there are several ways around the inability of the UITextView to resign as first responder using the keyboard. The usual method is to place a Done button in the navigation bar when the UITextView presents the pop-up keyboard. When tapped, this button asks the text view to resign as first responder, which will then dismiss the keyboard.

However, depending on how you’ve planned out your interface, you might want the UITextView to resign when the user taps outside the UITextView itself. To do this, you’d subclass UIView to accept touches, and then instruct the text view to resign when the user taps outside the view itself.

Create a new class,

#import <UIKit/UIKit.h>

@interface CustomView : UIView {
   IBOutlet UITextView *textView;
}

@end

Then, in the implementation, implement the touchesEnded:withEvent: method and ask the UITextView to resign as first responder.

#import "CustomView.h"

@implementation CustomView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}

- (void) awakeFromNib {
    self.multipleTouchEnabled = YES;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches began count %d, %@", [touches count], touches);

    [textView resignFirstResponder];
    [self.nextResponder touchesEnded:touches withEvent:event];
}

@end

Once you’ve added the class, you need to save all your changes, then go into Interface Builder and click on your view. Open the Identity inspector in the Utility pabel and change the type of the view in your nib file to be your CustomView rather than the default UIView class. Then in the Connections Inspector, drag the textView outlet to the UITextView. After doing so, and once you rebuild your application, touches outside the active UI elements will now dismiss the keyboard. Note however that if the UIView you are subclassing is “behind” other UI elements, these elements will intercept the touches before they reach the UIView layer. So while this solution is elegant, it can be used in only some situations. In many cases, you’ll have to resort to the brute force method of adding a Done button to the navigation bar to dismiss the keyboard.

战皆罪 2024-07-17 14:27:54

使用导航控制器并在完成后弹出视图?

例如,我使用这样的代码将“关于”框滑入:

[[self navigationController] presentModalViewController:modalViewController animated:YES];

然后当单击该“关于”框中的按钮时,我用它来摆脱它:

[self.navigationController dismissModalViewControllerAnimated:YES];

在我的情况下,“关于”框占据了整个屏幕,但我不这样做我认为这并不需要它才能起作用。

编辑:我想我可能误解了你的问题。 如果您自己伪造整个键盘视图,则与我的代码类似。 我认为,如果它是普通键盘,并且在顶部添加了工具栏,那么辞职第一响应者是正确的方法。

use a navigation controller and pop the view when done?

for example, I use code like this to slide an about box in:

[[self navigationController] presentModalViewController:modalViewController animated:YES];

and then when the button in that about box is clicked, I use this to get rid of it:

[self.navigationController dismissModalViewControllerAnimated:YES];

In my case the about box occupies the whole screen, but I don't think it would have to for this to work.

edit: I think I may have misunderstood your question. Something along the lines of my code would be if you are faking the whole keyboard view yourself. I think that resign first responder is the right way to do it if it is the normal keyboard with your toolbar added on top.

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