【问题标题】:how can I dismiss keyboard when I will just touch anywhere except UITextField?当我只触摸除 UITextField 之外的任何地方时,如何关闭键盘?
【发布时间】:2011-06-23 21:40:01
【问题描述】:

你好 我的应用程序中有两个 UITextFields,当我触摸除 UITextFields 之外的任何地方时,我想关闭键盘 我该怎么做?

【问题讨论】:

    标签: iphone keyboard uitextfield numeric-keypad


    【解决方案1】:

    在视图控制器中保留对 UITextField 的引用并调用 [yourTextField resignFirstResponder]

    【讨论】:

    • 引用是什么意思,怎么编码,请多解释一下
    • 我的意思是只有一个 IBOutlet 到 UITextField,然后当在其他任何地方有触摸时,调用 [yourTextField resignFirstResponder]
    【解决方案2】:

    您需要在后台创建一个自定义按钮,然后将其连接到在您的UITextField 上调用resignFirstResponderIBAction 方法

    类似的东西

    编辑:

    因为您想以编程方式添加按钮而不是使用此代码

    - (void)viewDidLoad
    {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
        button.buttonType = UIButtonTypeCustom;
        [button addTarget:self action:(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        [button release];
    

    然后在此之后添加您的控制器代码。

    【讨论】:

    • 我所有的编码都是手动的,不是基于Interface Builder的,所以我怎么能简单地调用一个方法,你能告诉我吗?
    • 改为添加点击手势
    【解决方案3】:

    创建一个不可见的按钮,将其放在UITextField 后面,并在点击按钮时将resignFirstResponder 消息发送到您的文本字段。

    【讨论】:

    • 并使透明按钮覆盖整个视图,这样除了顶部的文本字段之外的任何触摸都会激活按钮。
    【解决方案4】:

    使用委托,

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
     [textField resignFirstResponder];
     return YES;
    }
    

    你也可以在你的控制器中创建一个方法

     -(IBAction)editingEnded:(id)sender{
        [sender resignFirstResponder]; 
    }
    

    然后在 IB 的 Connection Inspector 中将 Event "Did End On Exit" 连接到它。

    【讨论】:

    • 我正在创建一个这样的方法,但是现在如何在单击除我的 textFields 之外的任何位置时调用它 - (void) backgroundTap { [self.restaurant_name_textfield resignFirstResponder]; [self.amount_textfield resignFirstResponder]; }
    • 我所有的编码都是手动的,不是基于Interface Builder的,所以我怎么能简单地调用一个方法,你能告诉我吗?
    【解决方案5】:

    为此使用 tochesBegin 方法

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
      [yourFirstTextField resignFirstResponder];
      [yourSecondTextField resignFirstResponder];
    }
    

    您不需要任何其他东西。当您触摸视图中的任何位置时,两个 textField 都会辞职,无需知道哪个有焦点。当您触摸 textField 时,textField 会自行打开键盘。

    【讨论】:

    • 吉,它工作得很好,我已经接受了你的回答,谢谢:-)
    • 我还添加了“[super touchesBegan:touches withEvent:event];”以防万一。
    【解决方案6】:

    如果您不使用界面生成器,您可以手动连接事件,如下所述: How can I programmatically link an UIView or UIImageView with an event like "touch up inside"?

    如果您使用的是界面生成器,则无需创建背景按钮。

    你可以这样做:

    1. 选择您的视图(可能称为视图)
    2. 在属性检查器中切换到 (i) 选项卡
    3. 将类从 UIView 更改为 UIControl(这使您可以访问 Touch Down 事件)。
    4. Touch Down 事件与您的 backgroundTap 方法挂钩(Ctrl + 拖动)。
    5. 别忘了保存更改

    【讨论】:

      【解决方案7】:
      You need to define the action for your button click this way
      
      [infoButton addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
      
      
      And implement the click method ,as for example i have provided the animation on button click method.
      -(void)backButtonClicked
      {
      
          [UIView beginAnimations:@"animation" context:nil];
          [UIView setAnimationDuration:1.5];
          [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO]; 
          [self.navigationController popViewControllerAnimated:YES]; 
          [UIView commitAnimations];
      
      }
      
      
      Hope this will help you ,the action is without using IB.Its all done through coding
      

      【讨论】:

        【解决方案8】:

        Ishu 的回答很好。但我认为你也应该尝试一下:

        - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        
          [self.view endEditing:YES];
        }
        

        【讨论】:

        • 谢谢,我不知道为什么,但 resignFirstResponder 对我不起作用。这是一种享受。
        • @Deco 您是否已将您的响应者与 Interface Builder 中的正确 IBOutlet 相关联?
        【解决方案9】:

        使用这个。

        - (void) animateTextField: (UITextField*) textField up: (BOOL) up
        {
        
            const int movementDistance = 180; distace of keyboard up.
        
            const float movementDuration = 0.3f;
        
             int movement = (up ? -movementDistance : movementDistance);
        
            [UIView beginAnimations: @"anim" context: nil];
            [UIView setAnimationBeginsFromCurrentState: YES];
        
            [UIView setAnimationDuration: movementDuration];
        
            self.view.frame = CGRectOffset(self.view.frame, 0, movement);
        
            [UIView commitAnimations];
        }
        

        【讨论】:

          【解决方案10】:

          通常在这种情况下,您将有一个滚动视图来保存用户界面的其余部分。如果您将该滚动视图的委托分配给您的视图控制器,并说您实现了协议UIScrollViewDelegate,那么只需添加以下方法即可完成工作:

          - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
          {
              [self.view endEditing:YES];
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-11-23
            • 2011-07-15
            • 2014-01-23
            • 2015-10-26
            • 1970-01-01
            • 2014-12-30
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多