點擊搜索取消UISearchDisplayController的搜索狀態

 一般,我們用到UISearchDisplayController的時候,都是需要對一個數據源進行刷選,在UISearchDisplayController自帶的tableView中展示出來,然後點擊退出詳情.我最近在做大衆點評第三方的時候,遇到一個問題,我展示出了全部商店,想加搜索功能,但是不知道輸入的搜索關鍵字去跟什麼匹配,大衆點評也並沒有提供一個用來刷選的數據源接口,這樣,我只能自己輸入關鍵字,不用它數據源提供刷選補全內容,而直接點擊彈出鍵盤的搜索鍵,進行搜索.不過點擊搜索後,UISearchDisplayController的搜索狀態並不會取消,這並不是我想要了效果,調試了很久,最終還是解決了.

[objc] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. #pragma mark - 點擊搜索,能夠取消搜索狀態  
  2. #pragma mark UISearchDisplayDelegate  
  3. - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller  
  4. {  
  5.     for (UIView *view in controller.searchBar.subviews)  
  6.     {  
  7.          NSLog(@"%d__|---%@",__LINE__,view);  
  8.         for (UIView *subView in view.subviews)  
  9.         {  
  10.              NSLog(@"%d__|!!!%@",__LINE__,subView);  
  11.             //  獲取"取消"按鈕  
  12.             if([subView isKindOfClass:[UIButton class]])  
  13.             {  
  14.                 UIButton *cancelButton = (UIButton *)subView;  
  15.                 //  獲取點擊"取消"按鈕的響應事件(actionsForTarget 這個方法返回的是一個數組)  
  16.                 self.cancelSearchSELString = [[cancelButton actionsForTarget:controller.searchBar forControlEvent:UIControlEventTouchUpInside] objectAtIndex:0];  
  17.                 //  響應通知,執行方法直接用上面獲得的響應事件方法,轉換一下(這是個知識點,可以擴展下)  
  18.                 [[NSNotificationCenter defaultCenter] addObserver:controller.searchBar selector:NSSelectorFromString(self.cancelSearchSELString) name:@"cancelSearch" object:nil];  
  19.             }  
  20.         }  
  21.     }  
  22. }  
  23.   
  24. #pragma mark UISearchBarDelegate------點擊搜索按鈕  
  25. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{  
  26.   
  27.     //  獲取你想搜索的最終完整關鍵字(一般可以用來做搜索歷史展示)    
  28.     NSLog(@"%s__%d__|%@",__FUNCTION__,__LINE__,searchBar.text);  
  29.     //  點擊按鈕時,發佈取消搜索狀態通知  
  30.     [[NSNotificationCenter defaultCenter] postNotificationName:@"cancelSearch" object:nil];  
  31.     //  發佈---響應---取消通知  
  32.     [[NSNotificationCenter defaultCenter] removeObserver:searchBar name:@"cancelSearch" object:nil];  
  33. }  


@擴展一下知識點:

[objc] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. 可以用actionsForTarget方法,來獲取針對某一特定事件目標的全部動作列表:  
  2. [java] view plaincopyprint?  
  3. NSArray* myActions = [ myControl actionForTarget:UIControlEventValueChanged ];    

[objc] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. //  一些特別的轉換  
  2. @class NSString, Protocol;  
  3.   
  4. FOUNDATION_EXPORT NSString *NSStringFromSelector(SEL aSelector);  
  5. FOUNDATION_EXPORT SEL NSSelectorFromString(NSString *aSelectorName);  
  6.   
  7. FOUNDATION_EXPORT NSString *NSStringFromClass(Class aClass);  
  8. FOUNDATION_EXPORT Class NSClassFromString(NSString *aClassName);  
  9.   
  10. FOUNDATION_EXPORT NSString *NSStringFromProtocol(Protocol *proto) NS_AVAILABLE(10_52_0);  
  11. FOUNDATION_EXPORT Protocol *NSProtocolFromString(NSString *namestr) NS_AVAILABLE(10_52_0);  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章