2014-02-18 94 views
0

你好我试图让我的两个UITextFields的键盘在用户点击键盘时隐藏。我需要两个文本域都有相同的方法。我怎样才能做到这一点,而不复制它们并给Xcode和错误? 谢谢!两个具有相同隐藏方法的UITextFields

下面是我用于我的第一个textfield的方法,我需要我的另一个相同的方法。

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

    UITouch *touch = [[event allTouches] anyObject]; 
    if ([text1 isFirstResponder] && [touch view] != text1) { 
     [text1 resignFirstResponder]; 
    } 
    [super touchesBegan:touches withEvent:event]; 
} 
+0

你试过了什么? – greymouser

回答

0

苹果公司强烈建议使用手势识别,没有的touchesBegan和其他“摩的”方法,如果你真的没有使用它们。

试试看看这个代码。它将适用于视图控制器内的任意数量的文本视图。

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
    UIGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onBackgroundTapped)]; 
    [self.view addGestureRecognizer:tapRecognizer]; 
... another initialization code 
} 

- (void)onBackgroundTapped 
{ 
    [self.view endEditing:YES]; 
} 
+0

工程就像一个魅力,非常感谢! – Jackintosh7