2015-06-17 185 views
0

我想直接添加搜索到我的導航欄(如Apple地圖)。導航欄搜索結果

enter image description here

所以這就是沒有問題,不適只是創建爲搜索欄:

searchBar.placeholder = "Gebäude und Räume suchen" 
    var rightNavBarButton = UIBarButtonItem(customView: searchBar) 
    self.navigationItem.rightBarButtonItem = rightNavBarButton 

但我應該怎麼解決才能看到效果?第一個病人嘗試使用Adaptive Popover,如果用戶輸入至少2個字母,則會加載彈出窗口並顯示結果(這樣我可以在iPad上顯示彈出窗口,並在iPhone上獲得全屏視圖) - 但在這裏我需要檢查每個按鍵,如果彈出窗口仍然可見,並且如果有一個自適應彈出窗口,我需要在子控制器上創建搜索欄。

所以我不知道如何解決像蘋果地圖(iPhone和iPad版本)這樣的事情 - 任何人都可以給我一些tipps我該怎麼做?這是一個Popover嗎?自定義視圖?

任何幫助將不勝感激。

編輯:

最簡單的方法(這就是它的ATM的樣子) - 我有一個搜索圖標的按鈕,如果用戶點擊這個搜索按鈕,生病負載的「搜索」控制器(與一個搜索欄) - 如果用戶點擊一個結果虐待「返回」,並顯示在地圖上的結果。

但我寧願讓它像蘋果地圖應用(與iPad和iPhone版本之間的差異)

+0

您是否實現了searchDisplayController? 您可以將該控制器設置爲:displaysSearchBarInNavigationBar這會使事情變得更容易。 – EmilDo

+0

謝謝 - 但UISearchController沒有屬性(searchDisplayController已被棄用) - 但希望有另一種「簡單」的方法來解決這些問題。我將檢查與UISearchController(atm我沒有使用任何控制器,因爲我的結果來自一個Web服務,我只能推他們到一個UITableView) – derdida

+0

那麼蘋果地圖應用程序只顯示一個UIPopover包含tableViewController,你能否詳細說明更多關於你想要看到的理想結果? – EmilDo

回答

1

我能看到爲什麼這是令人困惑的。您正在尋找的功能以前由UISearchDisplayControllerUISearchDisplayDelegate提供。但這些方法在iOS 8中不推薦使用。

這些日子的方式是使用UISearchController。文檔解釋得非常好:

您使用initWithSearchResultsController:方法創建一個新的搜索控制器,並傳入管理要顯示的內容的視圖控制器。 searchResultsUpdater屬性包含負責更新結果的對象。通常,searchResultsUpdater屬性中包含的對象與初始化期間設置的視圖控制器相同。但是,如果您創建了自己的模型對象來篩選和響應查詢,則可以將其設置爲searchResultsUpdater

提供和解除搜索結果的回調機制由UISearchControllerDelegate提供。

+0

謝謝你的回答! – derdida

1

在iPad上做的蘋果使用UIPopover,我以前使用下列代表做了同樣的事情自定義地圖應用:

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{ 
    //init a searchPopover with the view you want to show the results in and then call it 
    [self.searchPopover presentPopoverFromRect:[self.search bounds] inView:self.search permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
} 

- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar 
{ 
    [self.searchPopover dismissPopoverAnimated:YES];; 
} 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 

    //update the tableview that is in the searchPopover 
    //searchView is the view inside the popOver, filterResults is a custom method with an NSPredicate inside. 
    [self.searchView filterResultsUsingString:searchText]; 
} 

在視圖控制器將顯示的結果,你實現委派,這樣當用戶在該視圖中按下所需的按鈕或行時,父視圖不會被觸發,並且彈出窗口被取消,您可以放大所需的註釋。

-(void)didSelectResult:(id)annotation 
{ 
    //so when you select the result, and call this delegate method: 
    self.annotationToSelect = annotation; 
    [self.searchBar resignFirstResponder]; 
    self.search.text = @""; 
    [self.searchPopover dismissPopoverAnimated:YES]; 

} 
+0

感謝您分享您的代碼 - 我會嘗試一下! – derdida

3

您需要檢查一次。我認爲這個答案會幫助你。

Googlemap searchbar

請首先查看上面的鏈接。你會得到你的答案。它是在目標-c中,但你可以從中獲取參考。

-(void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar 
{ 
    [theSearchBar resignFirstResponder]; 
    CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
    [geocoder geocodeAddressString:theSearchBar.text completionHandler:^(NSArray *placemarks, NSError *error) { 
     //Error checking 

     CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
     MKCoordinateRegion region; 
     region.center.latitude = placemark.region.center.latitude; 
     region.center.longitude = placemark.region.center.longitude; 
     MKCoordinateSpan span; 
     double radius = placemark.region.radius/1000; // convert to km 

     NSLog(@"[searchBarSearchButtonClicked] Radius is %f", radius); 
     span.latitudeDelta = radius/112.0; 

     region.span = span; 

     [theMapView setRegion:region animated:YES]; 
    }]; 
}