2012-04-24 72 views
2

我使用MT.D列出了DialogViewController中的工作人員。 EnableSearch已打開,您可以篩選列表中的項目。但是,如果您推送到另一個視圖,則返回,搜索欄爲空。我能得到它恢復被覆蓋OnSearchTextChanged (string text)和字符串存儲到本地領域使用的搜索查詢,並在視圖回來成爲關注的焦點,我使用下面的代碼:MonoTouch.Dialog搜索欄返回到視圖時丟失搜索查詢

public override ViewDidAppear (bool animated) 
{ 
    base.ViewDidAppear (animated); 
    if (EnableSearch && !string.IsNullOrWhiteSpace (lastSearchQuery)) 
    { 
     this.SearchButtonClicked (lastSearchQuery); // this inserts text 
     this.StartSearch(); // no clue what this is doing 
     this.ReloadData(); // does nothing but was worth a try 
    } 
} 

該代碼插入文本到搜索欄並顯示它,但我不能讓它過濾,除非你鍵入一些東西。鍵盤進入視野,並有一個搜索按鈕,但它什麼都不做。有什麼建議麼?

回答

5

我想你所缺少的只是對DialogViewController上的PerformFilter的調用。

我輸入了一個快速示例來顯示行爲。我從來沒有看到你觀察到的確切行爲。我不必重新填充搜索字段。作爲參考,我使用Monotouch 5.2.11。

using System; 
using System.Linq; 
using MonoTouch.UIKit; 
using MonoTouch.Dialog; 
using MonoTouch.Foundation; 

namespace delete201204242A 
{ 
    [Register ("AppDelegate")] 
    public partial class AppDelegate : UIApplicationDelegate 
    { 
     UIWindow _window; 
     UINavigationController _nav; 
     MyDialogViewController _rootVC; 

     public override bool FinishedLaunching (UIApplication app, NSDictionary options) 
     { 
      _window = new UIWindow (UIScreen.MainScreen.Bounds); 

      RootElement _rootElement = new RootElement ("LINQ root element") { 
       new Section ("List") { 
        from x in new Expense [] { new Expense() {Name="one"}, new Expense() {Name="two"}, new Expense() {Name="three"} } 
        select (Element)new BindingContext (null, x, x.Name).Root 
       } 
      }; 

      _rootVC = new MyDialogViewController (_rootElement); 
      _rootVC.EnableSearch = true; 
      _nav = new UINavigationController (_rootVC); 

      _window.RootViewController = _nav; 

      _window.MakeKeyAndVisible(); 

      return true; 
     } 

     public class MyDialogViewController : DialogViewController 
     { 
      public MyDialogViewController (RootElement root) : base (root) {} 

      public string SearchString { get; set; }    
      public override void ViewDidAppear (bool animated) 
      { 
       base.ViewDidAppear (animated); 
       if (!string.IsNullOrEmpty (SearchString)) 
        this.PerformFilter (SearchString); 
      } 
      public override void OnSearchTextChanged (string text) 
      { 
       base.OnSearchTextChanged (text); 
       SearchString = text; 
      } 
     } 

     public class Expense 
     { 
      [Section("Expense Entry")] 

      [Entry("Enter expense name")] 
      public string Name; 
      [Section("Expense Details")] 

      [Caption("Description")] 
      [Entry] 
      public string Details; 
     } 

    } 
} 
+0

工作就像一個魅力!謝謝! – 2012-04-25 12:47:38