我是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