I am newbie to Monotouch and have added the following code to my SingleViewApplication. And you can see my controller code below, where I am trying to change the background after click of the button.
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.
}
It looks fine to me, but I dont see the background color changing in simulator.
Any ideas? Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…