【问题标题】:Hide Virtual Keyboard of UITextView when 'Done' Presses按下“完成”时隐藏 UITextView 的虚拟键盘
【发布时间】:2011-07-07 16:52:00
【问题描述】:

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

【问题讨论】:

    标签: ios iphone iphone-sdk-3.0 uitextview iphone-softkeyboard


    【解决方案1】:

    处理此问题的正确方法是将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];
    
    }
    

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

    【讨论】:

      【解决方案2】:

      我假设“完成”按钮是指返回键。它并不像你想象的那么直观。 This question 覆盖得很好。

      【讨论】:

        【解决方案3】:

        如果您希望能够使用返回键 [[self view] endEditing: YES];,可以将其添加到操作中

        【讨论】:

        • 我喜欢这个解决方案。 Xamarin.IOS 中的等效项是 View.EndEditing(true);
        【解决方案4】:

        确保声明支持UITextViewDelegate 协议。

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

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

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

        【讨论】:

          【解决方案5】:

          这是附件“完成”按钮的 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()
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-31
            • 1970-01-01
            • 2015-02-22
            • 1970-01-01
            • 2012-07-16
            • 2016-02-18
            相关资源
            最近更新 更多