2013-02-28 71 views
2

我需要更改取消按鈕的文本,以在我的應用程序中始終顯示「重置」。 我在這裏發現了很多similar問題,但所有的答案都是針對ObjC的,而我在Monotouch中工作。使用Monotouch更改UISearchBar上「取消」按鈕的文本?

+0

這將是更好,如果你能提供的一個答案(你可以回答自己的問題在於是,看見的FAQ)並將其標記爲回答 - 即幫助人們尋找答案。 – poupou 2013-02-28 13:24:40

回答

0

這是非常容易的 -

UIButton btnCancel = (UIButton)SearchBar.Subviews[3]; 
btnCancel.SetTitle ("test", UIControlState.Normal); 
+0

這不適用於iOS 7 – 2015-01-11 07:02:09

1

注意,子視圖層次是在iOS6的和ios7不同。

但通過添加以下你的擴展方法可以使用,在ios7和工作的ios5/iOS6的

searchBar.SetCancelTitle("NO!"); 

擴展方法:

public static class NimbleSearchBarExtensions 
{ 
    /// <summary> 
    /// Finds the Cancelbutton 
    /// </summary> 
    /// <returns></returns> 
    public static UIButton GetCancelButton(this UISearchBar searchBar) 
    { 
     //Look for a button, probably only one button, and that is probably the cancel button. 
     return searchBar.GetAllSubViews().OfType<UIButton>().FirstOrDefault(); 
    } 

    /// <summary> 
    /// Recursively traverses all subviews and returns them in a little list. 
    /// </summary> 
    /// <param name="view"></param> 
    /// <returns></returns> 
    public static IEnumerable<UIView> GetAllSubViews(this UIView view) 
    { 
     List<UIView> retList = new List<UIView>(); 
     retList.AddRange(view.Subviews); 
     foreach (var subview in view.Subviews) 
     { 
      retList.AddRange(subview.GetAllSubViews()); 
     } 

     return retList; 
    } 

    /// <summary> 
    /// Sets the title of the search bars cancel button 
    /// </summary> 
    /// <param name="searchBar"></param> 
    /// <param name="cancelTitle"></param> 
    public static void SetCancelTitle(this UISearchBar searchBar, string cancelTitle) 
    { 
     searchBar.GetCancelButton().SetTitle(cancelTitle,UIControlState.Normal); 
    } 
} 
2

使用下面的iOS7 +這樣的擴展方法:搜索欄.SetCancelTitle( 「關閉」);

它來源於KolbjørnBredrup的評論,這很棒,但我不清楚它內部使用的額外擴展方法(OfType和FirstOrDefault)的名稱空間是什麼,所以我將它們添加到了這個類中。

我也重命名類UISearchBarExtensions,以便它與我的其他人很好地坐在一起。

感謝KolbjørnBredrup原創!

與原來的,則它使用被稱爲:

searchBar.SetCancelTitle( 「關閉」);

public static class UISearchBarExtensions 
{ 
    /// <summary> 
    /// Finds the Cancelbutton 
    /// </summary> 
    /// <returns></returns> 
    public static UIButton GetCancelButton(this UISearchBar searchBar) 
    { 
     //Look for a button, probably only one button, and that is probably the cancel button. 
     return (UIButton)searchBar.GetAllSubViews().OfType<UIButton>().FirstOrDefault(); 
    } 

    public static UIView FirstOrDefault(this IEnumerable<UIView> views) 
    { 
     return ((List<UIView>)views)[0]; 
    } 

    public static IEnumerable<UIView> OfType<T>(this IEnumerable<UIView> views) 
    { 
     List<UIView> response = new List<UIView>(); 
     foreach(var view in views) 
     { 
      if(view.GetType() == typeof(T)) 
      { 
       response.Add(view); 
      } 
     } 
     return response; 
    } 

    /// <summary> 
    /// Recursively traverses all subviews and returns them in a little list. 
    /// </summary> 
    /// <param name="view"></param> 
    /// <returns></returns> 
    public static IEnumerable<UIView> GetAllSubViews(this UIView view) 
    { 
     List<UIView> retList = new List<UIView>(); 
     retList.AddRange(view.Subviews); 
     foreach (var subview in view.Subviews) 
     { 
      retList.AddRange(subview.GetAllSubViews()); 
     } 

     return retList; 
    } 

    /// <summary> 
    /// Sets the title of the search bars cancel button 
    /// </summary> 
    /// <param name="searchBar"></param> 
    /// <param name="cancelTitle"></param> 
    public static void SetCancelTitle(this UISearchBar searchBar, string cancelTitle) 
    { 
     searchBar.GetCancelButton().SetTitle(cancelTitle, UIControlState.Normal); 
    } 
} 
+0

「我不清楚它在內部使用的附加擴展方法的命名空間是什麼」 - 不需要所有自定義方法 - 只需使用:'使用System.Linq; – jbyrd 2017-08-21 13:55:53

相關問題