iphone - Monotouch.Dialog:启用 ScopeBar 时禁用自动搜索
<p><p>当我启用 <strong>ScopeBar</strong> 并在 <strong>Monotouch.Dialog</strong> 控件上连接 <strong>SelectedScopeButtonIndexChanged</strong> 时,自动搜索功能会在您启用设置 <strong>EnabledSearch=true</strong> 已禁用。</p>
<p>以下是它被禁用的方式:</p>
<pre><code> 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();};
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这中断搜索的原因是因为 DialogViewController 在 UISearchBar 上分配了一个自定义 UISearchBarDelegate。</p>
<p>当您连接到一个事件时,它会破坏委托(delegate)并将其替换为一个特殊的 UISearchBarDelegate,它将所有委托(delegate)方法转发给事件。</p>
<p>换句话说,您不能在同一个控件上混合和匹配委托(delegate)与事件。</p>
<p>一种可能的解决方案(如果您不想修补 MonoTouch.Dialog 本身)是用您自己的自定义 UISearchBarDelegate 替换现有的 sb.Delegate ,它可以实现 SearchScope 方法并将其他所有内容转发到已经存在的 UISearchBarDelegate之前设置的。</p>
<p>例如:</p>
<pre><code>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);
}
}
</code></pre></p>
<p style="font-size: 20px;">关于iphone - Monotouch.Dialog:启用 ScopeBar 时禁用自动搜索,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/14551279/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/14551279/
</a>
</p>
页:
[1]