【问题标题】:how to resign keyBoard as a firstResponder from a textView?如何从 textView 中将 keyBoard 作为 firstResponder 辞职?
【发布时间】:2010-06-10 14:59:59
【问题描述】:

我想作为第一响应者从文本视图中退出键盘。我希望用户在 textView 中编辑后点击返回键,键盘应该退出。但我不确定我应该怎么做? ?

【问题讨论】:

    标签: iphone objective-c cocoa-touch uikit


    【解决方案1】:

    希望此代码对您有所帮助。

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

    使用 UITextViewDelegate 协议和 yourTextViewName.delegate = self;

    【讨论】:

      【解决方案2】:

      如果您的类被设置为该文本视图的委托,只需在您的类代码中执行以下操作

      - (void)textViewTextDidEndEditing:(UITextView *)textView {
          [textView resignFirstResponder];
      }
      

      【讨论】:

      • 但是当我按下回车键时,它就到了下一行
      【解决方案3】:
      -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
      
      {
        if([text isEqualToString:@"\n"])
        [textView resignFirstResponder];
        return YES;
      }
      

      yourTextView.delegate=self; 还要添加,在 '.h' 文件中

      如果你没有添加 "if([text isEqualToString:@"\n"])" 你不能编辑

      【讨论】:

        【解决方案4】:

        确保已将文本字段设置为委托并将以下代码添加到您的实现文件中:

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

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-05
          • 2016-06-24
          相关资源
          最近更新 更多