2011-12-24 43 views
3

我學習iOS SDK使用this book和4章,當它告訴你如何隱藏數字鍵盤與背景輕拍我有一個問題。我這樣做的作者說,但沒有發生。我如何做到這一點? 什麼書的作者說:如何使用背景隱藏數字鍵盤點擊?

1)ViewController.h

- (IBAction)touchBackground:(id)sender; 

2創建新方法backgroundTap)在ViewController.m

-(void)touchBackground:(id)sender{ 
[nameField resignFirstResponder]; 
[numberField resignFirstResponder];} 

3)查看更改對象的階級身份加入方法在界面生成器(據我所知,在Xcode 4其控制)從UIView到UIControl

4)將事件列表中的TouchDown方法與File' s所有者和檢查方法backgroundTap。

這是我的代碼示例

附:我很抱歉我的英文,因爲我有俄文翻譯,所以我把所有的信息從這本書翻譯成英文。

回答

5

我想你可能會只是實現的方法是這樣爲了簡便:

- (IBAction)backgroundTouched:(id)sender { 
    [self.view endEditing:YES]; 
} 

使界面生成器後臺視圖UIControl,而不是UIView和事件掛鉤接觸到這個方法。

看看這個small example project。我在secondViewController中完成了這個操作,觸摸背景使鍵盤無法工作。您還可以看到我是如何改變的背景爲UIControl你可能想嘗試secondViewController.xib

3

的東西,而不會改變的背景看來,是:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [nameField resignFirstResponder]; 
    [numberField resignFirstResponder]; 
} 
1

你可能已經完成第4步(「連接事件列表中的TouchDown方法與文件的所有者和檢查方法backgroundTap「)不正確。在你的touchBackground方法中包含一個NSLog來檢查方法是否被調用。

-(void)touchBackground:(id)sender{ 
[nameField resignFirstResponder]; 
[numberField resignFirstResponder]; 
NSLog(@"touchBackground was called"); 
} 

如果消息(「touchBackground被稱爲」)在控制檯中顯示不出來,那麼你知道touchBackground不會被調用。