“完成”时隐藏 UITextView 的虚拟键盘压力机

发布于 2024-10-19 18:36:15 字数 259 浏览 6 评论 0原文

我想在按下“完成”时隐藏(resignFirstResponderUITextView的虚拟键盘。 UITextView 中没有“退出时结束”。在 UITextField 中,我将“退出时结束”与 IBAction 连接并调用 resignFirstResponder 方法。我怎样才能用UITextView做到这一点?

I want to hide (resignFirstResponder) the virtual keyboard of UITextView when 'Done' presses. Theres no 'Did End on Exit' in UITextView. In UITextField i connect the 'Did End on Exit' with an IBAction and call resignFirstResponder method. How can i do this with UITextView?

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

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

发布评论

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

评论(5

北座城市 2024-10-26 18:36:15

处理此问题的正确方法是将 inputAccessoryView 中的完成按钮添加到 UITextViewinputAccessoryView 是有时出现在键盘上方的栏。

为了实现 inputAccessoryView,只需添加此方法(或其变体)并在 viewDidLoad 中调用它。

- (void)addInputAccessoryViewForTextView:(UITextView *)textView{

//Create the toolbar for the inputAccessoryView
UIToolbar* toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[toolbar sizeToFit];
toolbar.barStyle = UIBarStyleBlackTranslucent;

//Add the done button and set its target:action: to call the method returnTextView:
toolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                       [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(returnTextView:)],
                       nil];

//Set the inputAccessoryView
[textView setInputAccessoryView:toolbar];

}

然后通过实现您使用 resignFirstResponder 调用的操作方法来处理按下的按钮。

- (void) returnBreakdown:(UIButton *)sender{

[self.textView resignFirstResponder];

}

这应该会导致键盘上方的标准工具栏中出现一个可用的“完成”按钮。

The correct way to handle this is to add a done button in an inputAccessoryView to the UITextView. The inputAccessoryView is the bar that sometimes appears above the keyboard.

In order to implement the inputAccessoryView simply add this method (or a variation thereof) and call it in viewDidLoad.

- (void)addInputAccessoryViewForTextView:(UITextView *)textView{

//Create the toolbar for the inputAccessoryView
UIToolbar* toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[toolbar sizeToFit];
toolbar.barStyle = UIBarStyleBlackTranslucent;

//Add the done button and set its target:action: to call the method returnTextView:
toolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                       [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(returnTextView:)],
                       nil];

//Set the inputAccessoryView
[textView setInputAccessoryView:toolbar];

}

Then handel the button being pressed by implementing the action method you called with resignFirstResponder.

- (void) returnBreakdown:(UIButton *)sender{

[self.textView resignFirstResponder];

}

This should result in a working "Done" button appearing in a standard toolbar above the keyboard.

世俗缘 2024-10-26 18:36:15

我假设“完成”按钮指的是返回键。它并不像您想象的那么直观。 这个问题很好地涵盖了这一点。

I'm assuming by the "Done" button you mean the return key. It's not as intuitive as you might think. This question covers it pretty well.

半城柳色半声笛 2024-10-26 18:36:15

如果您希望能够使用返回键,您可以将其添加到操作中
[[自我视图]结束编辑:是];

you could add this to an action if you want to be able to use your return key
[[self view] endEditing: YES];

小女人ら 2024-10-26 18:36:15

确保您声明支持 UITextViewDelegate 协议。

.h 文件中的 @interface ...ViewController : UIViewController`。

在.m文件中,实现以下方法

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES; }

Make sure you declare support for the UITextViewDelegate protocol.

@interface ...ViewController : UIViewController` in .h file.

In .m file, implement below method

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES; }
痞味浪人 2024-10-26 18:36:15

这是附件“完成”按钮的 Swift 版本:

@IBOutlet weak var textView: UITextView!

// In viewDidLoad()

    let toolbar = UIToolbar()
    toolbar.bounds = CGRectMake(0, 0, 320, 50)
    toolbar.sizeToFit()
    toolbar.barStyle = UIBarStyle.Default
    toolbar.items = [
        UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
        UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: nil, action: "handleDone:")
    ]

    self.textView.inputAccessoryView = toolbar

// -----------------

func handleDone(sender:UIButton) {
    self.textView.resignFirstResponder()
}

Here is the Swift version of the accessory "Done" button:

@IBOutlet weak var textView: UITextView!

// In viewDidLoad()

    let toolbar = UIToolbar()
    toolbar.bounds = CGRectMake(0, 0, 320, 50)
    toolbar.sizeToFit()
    toolbar.barStyle = UIBarStyle.Default
    toolbar.items = [
        UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
        UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: nil, action: "handleDone:")
    ]

    self.textView.inputAccessoryView = toolbar

// -----------------

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