2014-02-18 34 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