I need to change the text of the Cancel button to always display "Reset" in my app. I found many similar questions here on SO, but all answers are for ObjC, while I am working in Monotouch.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Use the extension method below for iOS7+ like this: searchBar.SetCancelTitle("close");
It derives from Kolbjørn Bredrup's comment, which is great, but it wasn't clear to me what the namespaces were for the additional extension methods that it uses internally (OfType and FirstOrDefault) so I added them to this class myself.
I also renamed the class UISearchBarExtensions so that it sat nicely with my other ones.
Thanks to Kolbjørn Bredrup for the original!
As with the original, it is called using:
searchBar.SetCancelTitle("close");
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);
}
}
回答2:
Note that the subview hierarchy is different in ios6 and ios7.
But by adding the extension method below you can use
searchBar.SetCancelTitle("NO!");
Extension method that works in ios7 and ios5/ios6:
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);
}
}
回答3:
It was very easy -
UIButton btnCancel = (UIButton)SearchBar.Subviews[3];
btnCancel.SetTitle ("test", UIControlState.Normal);