2011-12-16 67 views
9

使用一個UISearchBar與showCancelButton = YES在iOS 5,想取消按鈕,以保持啓用狀態時,鍵盤滴下來。使用下面的代碼似乎不起作用:的UISearchBar取消按鈕

for (id subView in self.searchControl.subviews) 
    { 
     if ([subView isKindOfClass:[UIButton class]]) 
     { 
      UIButton *cancelButton = (UIButton *)subView; 
      [cancelButton setEnabled:YES]; 
      break; 
     }   
    } 

子視圖實際上是出現不被繼承關的UIButton的UINavigationButton。我在這裏想念什麼???????在Apple文檔中也找不到UINavigationButton類中的任何信息。

+0

截至目前,沒有一個答案解釋你應該如何尋找取消按鈕。 Byte的答案,例如,只是使用isKindOfClass:OP已經說過的UIButton不起作用。由於UINavigationButton是一個未公開的類,那麼這是否意味着發送到該對象的任何消息都會導致您的應用程序被拒絕? – 2014-02-17 22:51:13

回答

1

我必須解決我的應用程序相同的問題。嘗試在分數延遲後執行上述代碼,例如

[self performSelector:@selector(delayedEnable:) withObject:cancelButton afterDelay:0.001]; 

- (void)delayedEnable:(UIButton*)button { 
    button.enabled = YES; 
} 

這很醜陋,但這就是它爲我工作。或者,如果你真的使用UISearchDisplayController來顯示結果,也應該修正取消按鈕的行爲對你(我想 - 我已經開始鑽研這個問題更少)。

+0

這就像一個魔術.. @ Kurt你能告訴我你從哪裏得到這個? Amazing..upvoted – NSPratik 2016-12-19 12:31:23

2

只是爲了改進都庫爾特·斯平德勒在他的文章中說。雖然這可能不是優越的,但它更受控制。我使用dispatch來做同樣的事情。

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{ 
    for (UIView *subview in searchBar.subviews) 
    { 
     if ([subview isKindOfClass:[UIButton class]]) 
     { 
      int64_t delayInSeconds = .001; 
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
       UIButton * cancelButton = (UIButton *)subview; 
       [cancelButton setEnabled:YES]; 
      }); 
      break; 
     } 
    } 
} 

這應該適用於需要幫助的所有人保持取消啓用。確保稍後使用取消或點擊搜索來隱藏它。

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ 
    [searchBar resignFirstResponder]; 
    [searchBar setShowsCancelButton:NO animated:YES]; 
} 
+0

如果您想異步執行某些操作,只需使用dispatch_async即可。您不需要使用「幾乎即時延遲值」的dispatch_after。例如。在這種情況下`dispatch_async(dispatch_get_main_queue(),^(void){...});`工作得很好。 – 2012-12-25 17:49:25

2

我在搜索欄取消按鈕上放置了一個自定義取消按鈕。

16

設置你的搜索欄委託,比把這個代碼。

- (void) searchBarSearchButtonClicked:(UISearchBar*) theSearchBar 
    { 
    [theSearchBar resignFirstResponder]; 
    [theSearchBar setShowsCancelButton:NO animated:YES]; 
    } 
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
    { 
    [searchBar setShowsCancelButton:YES animated:YES]; 
    } 
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
    { 
    [searchBar resignFirstResponder]; 
    [searchBar setShowsCancelButton:NO animated:YES]; 
    } 

雨燕3.0

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 
    searchBar .resignFirstResponder() 
    searchBar.setShowsCancelButton(false, animated: true) 
} 
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { 
    searchBar.setShowsCancelButton(true, animated: true) 
} 
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 
    searchBar.resignFirstResponder() 
    searchBar.setShowsCancelButton(false, animated: true) 
} 
1

我知道這個人是老了,但我還是能夠解決它,我無法找到任何東西,這樣解決了這個問題對我來說一樣,所以這裏的什麼工作(在Swift 3中):

當鍵盤隱藏時啓用取消按鈕。這種加入viewDidLoad中():在keyboardNotification(通知:)方法

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil) 

,反應到keyboardDidHide通知:

func keyboardNotification(notification: NSNotification) { 
    if notification.name == Notification.Name.UIKeyboardDidHide { 
     self.enableSearchCancelButton() 
    } 
} 

的enableSearchCancelButton從別人怎麼回答here服用。

func enableSearchCancelButton() { 
    //enable the cancel button 
    for view1 in searchBar.subviews { 
     for view2 in view1.subviews { 
      if let button = view2 as? UIButton { 
       button.isEnabled = true 
       button.isUserInteractionEnabled = true 
      } 
     } 
    } 
} 

最後,不要忘記刪除視圖控制器作爲觀察員:

deinit { 
    NotificationCenter.default.removeObserver(self) 
}