【问题标题】:Dismiss Keyboard in one of multiple views在多个视图之一中关闭键盘
【发布时间】:2018-05-12 14:03:09
【问题描述】:

我在一个屏幕中有 3 个视图,其中包含不同的组件,例如表格和按钮,当我尝试使用下面的代码关闭键盘时,当我单击包含 textView( 这就是我需要! ) 。但是,当我单击其他视图之一时,它会关闭。

下图显示了我拥有的视图,滚动视图中的最后一个视图是包含 textView 的视图。

尽管视图被录音,我怎样才能让键盘关闭。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch * touch = [touches anyObject];

if(touch.phase == UITouchPhaseBegan) {
    [aTextField resignFirstResponder];
  }
}

此代码也无法关闭:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

[[self view] endEditing:TRUE];

}

【问题讨论】:

    标签: ios objective-c keyboard


    【解决方案1】:

    您需要在要点击的视图中添加点击手势: 因为您正在使用名称为 Your_view1 和 Your_view2 的两个 UIView,文本字段是 txt1 和 txt2。代码如下:

    你的视图1:

    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    singleTapGesture.numberOfTapsRequired = 1;
    [Your_view1 addGestureRecognizer:singleTapGesture];
    

    你的视图2:

    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    singleTapGesture.numberOfTapsRequired = 1;
    [Your_view2 addGestureRecognizer:singleTapGesture];
    

    代表是:

    -(void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTapGesture{
        [txt1 resignFirstResponder];
        [txt2 resignFirstResponder];
    }
    

    【讨论】:

      【解决方案2】:

      如果您的 textView 是可编辑的,点击它,将使其成为第一响应者。这就是键盘不关闭的原因。如果不需要编辑 textView,请将其 editable 属性设置为 false

      【讨论】:

      • 感谢您的回答,但我希望 textView 是可编辑的。
      • @DvixExtract 嗯,我不明白。用户点击 textView,所以他想编辑它。但是你想隐藏键盘。什么?
      • 所以用户点击文本视图并输入文本,然后他们应该点击屏幕上的任意位置以便键盘关闭。
      • @DvixExtract 你说我现在工作。根据您的问题:“但是,当我点击其他视图时,它会关闭”
      • 看到我有 3 个视图,如图所示。当我点击其中一个不包含 textView 的视图时,它会关闭键盘。我想要的是点击包含 textView 的视图,以便它关闭键盘。
      【解决方案3】:

      您应该查看UIScrollView 的 Apple 文档。

      因为滚动视图没有滚动条,所以它必须知道触摸是表示滚动意图还是跟踪内容中的子视图的意图。为了做出这个决定,它会通过启动一个定时器来临时拦截一个触摸事件,并在定时器触发之前,查看触摸的手指是否有任何动作。

      这就是为什么当您在滚动视图中单击时touchesBegan 不起作用的原因。

      修复它的简单方法,为您的viewController.view 添加UITapGestureRecognizer

      @property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer;
      
      - (void)viewDidLoad {
        [super viewDidLoad];
        self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.aTextField action:@selector(resignFirstResponder)];
        [self.view addGestureRecognizer:self.tapGestureRecognizer];
      }
      

      【讨论】:

        【解决方案4】:

        管理键盘占用了我们大部分的开发时间,幸运的是,我们有一个替代解决方案,library

        无需在每个视图控制器中编写多行代码,只需在 didFinishLaunchingWithOptions 中添加一行代码

        IQKeyboardManager.sharedManager().enable = true
        

        这样可以节省时间 希望对你有帮助

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-18
          • 1970-01-01
          • 1970-01-01
          • 2014-04-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多