IOS开发之触摸背景关闭键盘的代码实现

IOS开发之触摸背景关闭键盘的代码实现       

 

    直接上代码:

 

  1. // 触摸背景,关闭键盘 
  2. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
  3.     UITouch *touch = [touches anyObject]; 
  4.     UIView *view = (UIView *)[touch view]; 
  5.     if (view == self.view) { 
  6.         [weightTextField resignFirstResponder]; 
  7.     } 
// 触摸背景,关闭键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    UIView *view = (UIView *)[touch view];
    if (view == self.view) {
        [weightTextField resignFirstResponder];
    }
}
    以上代码是在一个viewController里面,if语句中的判断self.view指的就是背景。

    touchesBegan:withEvent:是声明在UIResponder中的一个函数,当一个或多个手指在view或者window上触摸时会调用该函数。默认没有做任何事情。这里重写它。

posted on 2013-01-07 12:05  静静的白色外套  阅读(221)  评论(0)    收藏  举报

导航