ios - 为什么 UIButton 在以编程方式调用其处理程序时不更改其图像?

标签 ios xamarin.ios uibutton

出于某种原因,当尝试通过 Stop() 方法以编程方式激活按钮时,按钮图像不会变回与正常状态关联的图像,直到我再次用鼠标单击该按钮。有什么想法吗?

    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);
    }

最佳答案

您必须从按钮的主线程发送操作。

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

关于ios - 为什么 UIButton 在以编程方式调用其处理程序时不更改其图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10668715/

相关文章:

ios - Mobile Safari - 输入插入符不随溢出滚动一起滚动 : touch

xamarin - Web View 中的 Cookie xamarin iOS

ios - 如何播放.ism/manifest文件?

Xamarin.iOS 链接器和反射问题

ios - 是否可以在类里面从 IB 获得按钮标题?

swift - 表格 View 单元格中的按钮导致不必要的滚动

ios - RxSwift 和 UICollectionView、UITableView

ios - 当URL实际存在时,NSURLConnection命中didFailWithError

ios - 当 View Controller 消失时会调用 deinit 吗?

ios - 以编程方式设置按钮的宽度和高度