nbonwit
nbonwit

Reputation: 305

Why does UIButton not change its image when its handler is called programmatically?

For some reason, when trying to activate the button programmatically via the Stop() method, the button image does not change back to the image associated with the normal state until I click on the button again with the mouse. Any ideas?

    public void Stop()
    {
        buttonStartStop.SendActionForControlEvents(UIControlEvent.TouchUpInside);
    }

    partial void actionButtonStartStopPress(MonoTouch.Foundation.NSObject sender)
    {
        Console.WriteLine("StartStop Button State On Entering Handler: " + buttonStartStop.State);
        if(buttonStartStop.State == UIControlState.Highlighted)
        {
            buttonStartStop.Selected = true;
            buttonStartStop.Highlighted = true;
            buttonLiveHome.Enabled = false;
            buttonLiveBack.Enabled = false;
            buttonCalibrate.Enabled = false;
            MainLoop.StreamData(true);
        }
        else
        {
            buttonStartStop.SetTitle("Start", UIControlState.Normal);
            buttonStartStop.Selected = false;
            buttonStartStop.Highlighted = false;
            buttonLiveHome.Enabled = true;
            buttonLiveBack.Enabled = true;
            buttonCalibrate.Enabled = true;
            MainLoop.StreamData(false);
        }

        Console.WriteLine("StartStop Button State On Exiting Handler (0 means Normal): " + buttonStartStop.State);
    }

Upvotes: 1

Views: 461

Answers (1)

holmes
holmes

Reputation: 1341

You must send the action from the main thread for the button.

public void Stop()
{
    buttonStartStop.InvokeOnMainThread (new NSAction (()=> {
        buttonStartStop.SendActionForControlEvents(UIControlEvent.TouchUpInside);
    }));
}

Upvotes: 1

Related Questions