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.
I am also face this problem but i solve this issue hope this code is help you
Also from ViewDidload Method Call this above method on UIButton Click events
This example was taken from one of the MonoTouch books and I had to convert the source code to work on the latest release for MonoTouch.
The original source was referring subView as
And in the process I failed to create an outlet for the view, i.e subView.
If there is no outlet created, the view can be accessed in the following fashion also
and it fixed the problem.
Make sure your code is actually running in your eventHandler by adding a breakpoint or a console-output. If this works out, maybe you're just missing an "SetNeedsDisplay".
Try this: