我是MonoTouch的新手,并且已将以下代码添加到我的SingleViewApplication中。你可以在下面看到我的控制器代码,点击按钮后,我试图改变背景。如何在Monotouch中更改UIButton的背景颜色
public partial class FirstViewAppViewController : UIViewController
{
public FirstViewAppViewController() : base ("FirstViewAppViewController", null)
{
}
UIButton buttonChangeColor;
private void CreateButton(){
RectangleF viewFrame = this.View.Frame;
RectangleF buttonFrame = new RectangleF (10f, viewFrame.Bottom -
200f, viewFrame.Width - 20f, 50f);
this.buttonChangeColor = UIButton.FromType
(UIButtonType.RoundedRect);
this.buttonChangeColor.Frame = buttonFrame;
this.buttonChangeColor.SetTitle ("Tap to change view color",
UIControlState.Normal);
this.buttonChangeColor.SetTitle ("Changing color...",
UIControlState.Highlighted);
this.buttonChangeColor.TouchUpInside +=
this.buttonChangeColor_TouchUpInside;
this.View.AddSubview (this.buttonChangeColor);
}
bool isRed;
private void buttonChangeColor_TouchUpInside (object sender, EventArgs e){
if (this.isRed) {
this.View.BackgroundColor = UIColor.LightGray;
this.isRed = false;
} else {
this.View.BackgroundColor = UIColor.Red;
this.isRed = true;
}
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.CreateButton();
// Perform any additional setup after loading the view, typically from a nib.
}
它看起来好像没什么问题,但我没有看到背景色模拟器变化。
任何想法?谢谢。
谢谢CHIFFRE为您的答案。我可以使用断点调试代码,并添加了控制台语句,它们都按预期工作,但即使在添加SetNeedsDisplay()后,背景颜色也不会改变。我已经在Lion OS上安装了最新版本的MonoTouch(稳定)版本。 – skjagini 2012-03-17 07:12:31