0

i use Monotouch.Dialog (Xamarin.iOS Version: 6.2.0.65) to create a RadioGroup with RadioElements. I only want the 'search' functionality in the second screen (where the user selects from the long list), so i created a separate DialogViewController where i enabled the search filter (enableSearch = true).

When typing in the search box, the app crashes in the GetCell method of the RadioElement.

"Object reference not set to an instance of an object" on line 1064 of Element.cs. Do i have GC problems? I managed to simulate it in a small app... Don't pay attention some weird field members, thats me testing if i'm begin 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);
    }           
}
}
Peesjee
  • 3
  • 1
  • I'm not sure this is it. But, you're custom Root Element doesn't look right. Is there a reason you're using a custom root element? Even if that's correct I don't see where you're actually creating the `RootElement` for `Root.add (sectionList);` Also, does this work if you don't have search enabled? – BRogers Mar 08 '13 at 17:52
  • Is this a dead question? – BRogers Mar 11 '13 at 01:13
  • No dead question, i'm just not working in the weekends... it works fine when no filter. The get cell crash is when using the filter. The RootElement is created through the constructor of LocatiesViewController. – Peesjee Mar 11 '13 at 07:35

2 Answers2

0

Try enabling the search after you instantiate the DialogViewController.

Like this :

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

Hopefully that will work for you.

BRogers
  • 3,534
  • 4
  • 23
  • 32
0

The bug report here includes a workaround for the root cause of the issue you're experiencing, but also talks about how filtering will then cause a usability issue of marking the nth element as selected even after a filter has been applied.

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

If you don't want to update the core MTD code, you could use that same technique by putting it in your own UIBarSearchDelegate. Unfortunately, the default SearchDelegate class is internal, so you'll need to add all of the code in your delegate. I was able to do this and get it working without changing the MTD source:

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

And then you use this instead of the base method:

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