Monotouch.Dialog:使用EnableSearch和自定义RootElement崩溃

时间:2013-03-08 12:56:24

标签: search crash monotouch.dialog

我使用Monotouch.Dialog(Xamarin.iOS版本:6.2.0.65)创建一个带RadioElements的RadioGroup。我只想在第二个屏幕(用户从长列表中选择)中使用'搜索'功能,所以我创建了一个单独的DialogViewController,我启用了搜索过滤器(enableSearch = true)。

在搜索框中输入内容时,应用程序会在RadioElement的GetCell方法中崩溃。

在Element.cs的第1064行上

“对象引用未设置为对象的实例”。我有GC问题吗?我设法在一个小应用程序中模拟它...不要注意一些奇怪的现场成员,这就是我测试我是否开始GC ......

using MonoTouch.Dialog;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace SearchCrash
{
// The UIApplicationDelegate for the application. This class is responsible for launching the 
// User Interface of the application, as well as listening (and optionally responding) to 
// application events from iOS.
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    // class-level declarations
    UIWindow window;
    public UINavigationController NavigationController { get; set; }

    //
    // This method is invoked when the application has loaded and is ready to run. In this 
    // method you should instantiate the window, load the UI into it and then make the window
    // visible.
    //
    // You have 17 seconds to return from this method, or iOS will terminate your application.
    //
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        window = new UIWindow(UIScreen.MainScreen.Bounds);
        this.NavigationController = new UINavigationController();
        this.NavigationController.PushViewController(new FirstViewController(this.NavigationController), true);
        window.RootViewController = this.NavigationController;
        window.MakeKeyAndVisible();

        return true;
    }
}

public class FirstViewController : DialogViewController
{
    private UINavigationController controller;
    private LocatiesRootElement locRootElement;
    private RadioGroup radioGroup;

    public FirstViewController(UINavigationController c) : base(new RootElement("Test"), true)
    {
        this.controller = c;
    }

    public override void ViewDidLoad()
    {
        Section section = new Section();
        radioGroup = new RadioGroup(0);
        locRootElement = new LocatiesRootElement("test", radioGroup, this.controller);
        section.Add(locRootElement);
        Root.Add(section);
    }
}

public class LocatiesRootElement : RootElement
{
    private UINavigationController navigationController;
    private LocatiesViewController locatiesViewController;

    public LocatiesRootElement(string selectedLocatie, RadioGroup group, UINavigationController controller) : base("Locatie", group)
    {
        this.navigationController = controller;
        this.locatiesViewController = new LocatiesViewController(this);
    }

    public override void Selected(DialogViewController dvc, UITableView tableView, NSIndexPath path)
    {
        this.navigationController.PushViewController(this.locatiesViewController, true);
    }
}

public class LocatiesViewController : DialogViewController
{
    public LocatiesViewController(LocatiesRootElement root) : base(root, true)
    {
        this.EnableSearch = true;
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        Section sectionList = new Section();

        RadioElement radioElement1 = new RadioElement("item 1");
        RadioElement radioElement2 = new RadioElement("item 2");
        sectionList.Add(radioElement1);
        sectionList.Add(radioElement2);

        Root.Add(sectionList);
    }           
}
}

2 个答案:

答案 0 :(得分:0)

在实例化DialogViewController

后尝试启用搜索

像这样:

public LocatiesRootElement(string selectedLocatie, RadioGroup group, UINavigationController controller) : base("Locatie", group)
    {
        this.navigationController = controller;
        this.locatiesViewController = new LocatiesViewController(this);
        this.locatiesViewController.EnableSearch = true;
    }

希望这对你有用。

答案 1 :(得分:0)

此处的错误报告包含针对您遇到的问题的根本原因的解决方法,还介绍了过滤如何导致将第n个元素标记为选中的可用性问题,即使在应用了过滤器之后也是如此

https://github.com/migueldeicaza/MonoTouch.Dialog/issues/203

如果您不想更新核心MTD代码,可以将它放在您自己的UIBarSearchDelegate中使用相同的技术。不幸的是,默认的SearchDelegate类是内部的,因此您需要在代理中添加所有代码。我能够做到这一点,并在不改变MTD源的情况下使其工作:

    public override void LoadView()
    {
        base.LoadView();
        ((UISearchBar)TableView.TableHeaderView).Delegate = new MySearchBarDelegate(this);
    }

然后你使用它而不是基本方法:

public override void TextChanged (UISearchBar searchBar, string searchText)
{
    container.PerformFilter (searchText ?? "");
    foreach (var s in container.Root)
        s.Parent = container.Root;
}