当我启用 ScopeBar 并在 Monotouch.Dialog 控件上连接 SelectedScopeButtonIndexChanged 时,将禁用在设置 EnabledSearch = true时启用的自动搜索功能。
禁用它的方法如下: UISearchBar sb = SearchBar;
if (sb != null)
{
sb.ScopeButtonTitles = new string[] { "Girl".t(),"Boy".t(),"All".t() };
sb.ShowsScopeBar = true;
sb.SizeToFit();
// THIS NEXT LINE KILLS SEARCH, remove to make it all work again
sb.SelectedScopeButtonIndexChanged+= (sender, e) => {Update();};
}
最佳答案
之所以中断搜索,是因为DialogViewController在UISearchBar上分配了一个自定义UISearchBarDelegate。
当您连接到事件时,它将取代Delegate并将其替换为特殊的UISearchBarDelegate,该UISearchBarDelegate将所有委托方法转发给事件。
换句话说,您不能在同一控件上将委托与事件混合和匹配。
一种可能的解决方案(如果您不想修补MonoTouch.Dialog本身),是用您自己的自定义UISearchBarDelegate替换现有的sb.Delegate,该自定义UISearchBarDelegate可以实现SearchScope方法并将其他所有内容转发到先前已经设置的UISearchBarDelegate。
例如:
public class MySearchBarDelegate : UISearchBarDelegate
{
UISearchBarDelegate original;
MyDialogViewController dvc;
public MySearchBarDelegate (MyDialogViewController dvc, UISearchBarDelegate original)
{
this.original = original;
}
public override void SelectedScopeButtonIndexChanged (UISearchBar searchBar, int selectedScope)
{
dvc.Update ();
}
public override void OnEditingStarted (UISearchBar searchBar)
{
original.OnEditingStarted (searchBar);
}
public override void OnEditingStopped (UISearchBar searchBar)
{
original.OnEditingStopped (searchBar);
}
public override void TextChanged (UISearchBar searchBar, string searchText)
{
original.TextChanged (searchBar, searchText);
}
public override void CancelButtonClicked (UISearchBar searchBar)
{
original.CancelButtonClicked (searchBar);
}
public override void SearchButtonClicked (UISearchBar searchBar)
{
original.SearchButtonClicked (searchBar);
}
}