2010-01-30 49 views
1

我的導航欄中有一個充當搜索框的UITextView。我想在用戶點擊文本框之下時關閉相關的鍵盤 - 即在MKMapView上。不過,我不知道如何做到這一點,因爲它看起來不像我可以攔截來自mapview的觸摸。當用戶點擊Mapview時關閉鍵盤

我已經看過一些解決方案,但似乎沒有爲我的情況工作,據我所知。有沒有人有一個簡單的方法來做到這一點?我有一點小菜,所以如果我沒有提供相關信息,請告訴我,如果可以的話,請在答案中提供幾行示例代碼 - 我用術語仍然有點不穩定。謝謝!

screenshot http://img532.imageshack.us/img532/4070/keyboardsm.png

回答

1

假設

UITextView *textView; 

然後你就可以通過發送取消鍵盤:

[textView resignFirstResponder]; 

另外:你可能更願意使用UISearchBar並將其設置爲的的navigationItem.titleView視圖控制器。這提供了一些不錯的委託方法。

+0

感謝您使用UISearchBar的提示。我會嘗試。我的問題是,我不知道如何檢測用戶何時單擊mapview,所以我無處可以寫這行代碼:) – Imran 2010-01-30 17:44:45

+0

在添加代碼獲取觸摸。 – FelixLam 2010-01-30 18:22:46

+0

我不認爲你可以做到這一點!請參閱:http://stackoverflow.com/questions/1049889/how-to-intercept-touches-events-on-a-mkmapview-or-uiwebview-objects。如果我誤解,請告訴我。我正在考慮在鍵盤啓動時覆蓋地圖上的透明視圖,並從中獲取相關信息(我的鏈接中接受的解決方案看起來過於複雜)。 – Imran 2010-01-30 18:50:33

1

我知道這是一箇舊的,但如果其他人正在尋找一個簡單的答案,這是我的解決方案,以獲得一個「mapView」resignFirstResponder。這將以類似於iphone上的Google地圖應用的方式工作,其中在開始編輯搜索文本字段時出現半透明框。

首先,你需要讓你的視圖控制器UISearchBarDelegate

@interface ViewControllerName <UISearchBarDelegate> 

// your code here 

@end 

然後執行以下的委託方法:

@implementation ViewControllerName 

// your code here 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
darkBg = [[UIControl alloc] initWithFrame:CGRectMake(0, 244, 320, 300)]; 
[darkBg setBackgroundColor:[UIColor blackColor]]; 
[darkBg addTarget:nil action:@selector(hideKeyboard) forControlEvents:UIControlEventTouchDown]; 
[darkBg setAlpha:0.8]; 
[UIView beginAnimations:@"slideup" context:nil]; 
[darkBg setCenter:CGPointMake(160, 194)]; 
[self.view addSubview:darkBg]; 
[UIView commitAnimations]; 
} 

- (void)hideKeyboard { 
[UIView beginAnimations:@"fadeou" context:nil]; 
[darkBg setAlpha:0.0]; 
[UIView commitAnimations]; 
[addressField resignFirstResponder]; 
[darkBg performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0]; 
[darkBg release]; 
} 
0

一少哈克的解決辦法是依靠的的MKMapView委託方法一關閉鍵盤

- (void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated { 
    if ([self.textField isFirstResponder]) { 
    [self.textField resignFirstResponder]; 
    } 
}