【问题标题】:UItextView dismiss keyboard on tap outside the textviewUItextView 在文本视图之外点击关闭键盘
【发布时间】:2012-05-16 04:02:32
【问题描述】:

我有一个custom UIView,它是由UIViewController 提供的。这个视图控制器可以呈现几个这样的视图之一。其中一个视图具有UITextView。当一个人开始在UITextView 中输入文本时,会显示键盘。但是,每当用户点击文本视图之外的任何位置时,我都想关闭键盘。由于多个UIViews 可以由UIViewController 基于特定条件呈现,我将UITextView 委托方法等封装在custom UIView 中,我使用custom UIView 中的通知来做到这一点,就像

- (void)configureView
{ 
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

[nc addObserver:self selector:@selector(keyboardWillShow:) name:
 UIKeyboardWillShowNotification object:nil];

[nc addObserver:self selector:@selector(keyboardWillHide:) name:
 UIKeyboardWillHideNotification object:nil];

self.tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                           action:@selector(didTapAnywhere:)];
}

-(void) keyboardWillShow:(NSNotification *) note {
[self addGestureRecognizer:self.tapRecognizer];
}

-(void) keyboardWillHide:(NSNotification *) note
{
[self removeGestureRecognizer:self.tapRecognizer];
}

-(void)didTapAnywhere: (UITapGestureRecognizer*) recognizer {    
[self.detailsTextView resignFirstResponder];
}

但是,当调用 didTapAnywhere 时,即使 resignfirstresponder 绑定位置,键盘也不会被关闭。仅供参考,UITextView 的代表是custom UIView。

请帮助如何做到这一点

【问题讨论】:

  • 在其中抛出一个 NSLog() 以查看您的方法是否被调用。
  • 是的,当我点击 UITextView 之外的任何地方时,正在调用 didTapAnywhere

标签: ios uiview uitextview uikeyboard


【解决方案1】:

试试

[self.view endEditing:YES];

而不是让第一响应者辞职。这应该可以解决问题。 奇怪的是为什么标准方法不起作用......

【讨论】:

    【解决方案2】:

    您必须将UITextViewDelegate 包含在您的班级中,并将detailsTextView.delegate = self 包含在您的viewDidLoad 或您初始化您的 detailsTextView 的任何商品中。我希望这能解决您的问题...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 2021-12-20
      • 1970-01-01
      • 2021-09-25
      • 2014-09-23
      • 2018-07-24
      相关资源
      最近更新 更多