Search code examples
c#cocoa-toucheventsxamarin.iosuisegmentedcontrol

UISegmentedControl: no events fire when used in UIToolbar


Monotouch 4.2 beta.

I'm adding a UISegmentedControl to a UIToolbar. The problem is that none of the events fire when switching segments. What am I missing here?

this.oBookmarkSwitch = new UISegmentedControl(new UIImage[] { UIImage.FromFile("./images/index.png"), UIImage.FromFile("./images/bookmark_grey.png") });
this.oBookmarkSwitch.TouchUpInside += delegate {
Console.WriteLine("TOUCHY - never fires");
};
this.oBookmarkSwitch.ValueChanged += delegate {
Console.WriteLine("CHANGE - only fires once when the control is created");
};
this.oBookmarkSwitch.Frame = new RectangleF(0, 0, 100, 30);
this.oBookmarkSwitch.ControlStyle = UISegmentedControlStyle.Bar;
aBarButtons.Add(new UIBarButtonItem(this.oBookmarkSwitch));
oIndexToolbar.SetItems (aBarButtons.ToArray (), false);

Solution

  • Apple documentation is not very clear on this but it state that:

    "You register the target-action methods for a segmented control using the UIControlEventValueChanged constant as shown below." (emphasis mine)

    Also discussions from Apple and stackoverflow forums seems to indicate that only the ValueChange event is supported - which would match the runtime behavior. I.e. there's no MonoTouch specific code for the UISegmentedControl events (e.g. to remove support for inherited events like TouchUpInside).