【问题标题】:hiding keyboard ios [duplicate]隐藏键盘ios [重复]
【发布时间】:2012-05-10 11:52:15
【问题描述】:

我有一些文本输入,只要我触摸背景,我就可以隐藏键盘,但只有当我输入第一个文本框名称 textField1 时。现在这段代码应该很简单,但我似乎无法理解。

-(IBAction)backgroundTouched:(id)sender {
    [textField1 resignFirstResponder];
    [buildLength resignFirstResponder];
    [buildWidth resignFirstResponder];
    [ridgeWidth resignFirstResponder];
    [rafterWidth resignFirstResponder];
    [hipWidth resignFirstResponder];
    [eaveOverhang resignFirstResponder];
    [spacing resignFirstResponder];
}

【问题讨论】:

  • 什么对象正在接收backgroundTouched: 操作?是景色吗?你把什么东西放在一切后面?我在 iOS 上隐藏键盘的方法是覆盖 ViewController 的 touchesEnded:withEvent: 。当没有其他对象能够处理触摸事件时,它会被调用。在那里我辞去了第一响应者的职务,尽管您需要检查isFirstResponder,因为如果您不使用触摸,则应该调用super。
  • 同意@Ru​​ss...视图控制器上的触摸事件是更简单的方法。但它是否适用于 textField1 仍然很神秘。为什么不是其他人?我的猜测是其他句柄不好(例如,'buildLength' 没有正确初始化)。

标签: ios objective-c keyboard iphone-softkeyboard


【解决方案1】:

如果您想在点击按钮时隐藏键盘并且您的view 中有多个UITextFields,那么您应该使用:

[self.view endEditing:YES];

点击视图上的任意位置,键盘就会消失。

【讨论】:

  • 我注意到,在某些情况下,此方法在最多延迟约 0.3 秒后起作用。我的意思是,resignFirstResponder 会立即移除键盘。但是这个方法不是,你应该在使用这个之后延迟后使用 performSelector。
【解决方案2】:

试试这个:

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

【讨论】:

    【解决方案3】:

    您还可以遍历一组视图(例如您的 UIView 的子视图)并手动退出键盘,如果您不想退出父 UIView 中的所有子视图,这很好。

    - (void)viewDidLoad
    {
        self.view.userInteractionEnabled = TRUE;
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        //Iterate through your subviews, or some other custom array of views
        for (UIView *view in self.view.subviews)
            [view resignFirstResponder];
    }
    

    【讨论】:

      【解决方案4】:

      您可以尝试UITouch 方法,并在此设置您的文本字段对象并调用resignFirstResponder 每当您触摸屏幕时,键盘都会退出,我希望这对您有用。

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

      【讨论】:

        猜你喜欢
        • 2013-01-12
        • 1970-01-01
        • 2016-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-23
        • 2015-01-17
        • 1970-01-01
        相关资源
        最近更新 更多