2012-03-16 76 views
0

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

它看起来好像没什么问题,但我没有看到背景色模拟器变化。

任何想法?谢谢。

回答

3

我也面临这个问题,但我解决了这个问题,希望这个代码是帮助你

public static class SwapUIButtonColor 
{ 
#region prop 
public static UIButton PreviousButton { 
get 
{ 
return _previousButton 
} 
set 
{ 
_previousButton = value; 
} 
} 
public static UIButton CurrentButton { 
get{ 
return _currentButton; 
} 
set{ 
_currentButton = value; 
} 
} 
public static bool SwapColor (UIButton sender, bool isPrevBakGraound) 
{ 
if (isPrevBakGraound == false){ 
InitApplyColorBorder (sender); 
SwapUIButtonColor.PreviousButton = sender; 
return true; 
} 
else{ 
if (SwapUIButtonColor.PreviousButton != sender){ 
InitApplyColorBorder (sender); 
SwapUIButtonColor.CurrentButton = PreviousButton; 
PreviousButton = sender; 
SwapUIButtonColor.CurrentButton.Layer.BorderColor = (UIColor.Black).CGColor; 
SwapUIButtonColor.CurrentButton.Layer.BorderWidth = 0f; 
SwapUIButtonColor.CurrentButton.Layer.CornerRadius = 0f;  
} 
else if (SwapUIButtonColor.PreviousButton == sender){ 
InitApplyColorBorder (sender); 
SwapUIButtonColor.PreviousButton = sender; 
} 
return true ; 
} 
static void InitApplyColorBorder (UIButton sender){ 
sender.Layer.BorderColor = (UIColor.Red).CGColor; 
sender.Layer.BorderWidth = 1.0f; 
sender.Layer.CornerRadius = 10f; 
} 
} 

也从viewDidLoad方法调用上的UIButton这上面的方法Click事件

private bool isPrevBakGraound = false; 
isPrevBakGraound = SwapUIButtonColor.SwapColor (btnReset, isPrevBakGraound); 
0

通过添加断点或控制台输出,确保您的代码实际上正在您的eventHandler中运行。如果这可以解决,也许你只是错过了“SetNeedsDisplay”。

试试这个:

private void buttonChangeColor_TouchUpInside (object sender, EventArgs e){ 
    if (this.isRed) { 
    Console.WriteLine("isRed"); //Debug-Output 
    this.View.BackgroundColor = UIColor.LightGray; 
    this.isRed = false; 
    } else { 
    Console.WriteLine("isNotRed"); // Debug-Output 
    this.View.BackgroundColor = UIColor.Red; 
    this.isRed = true; 
    } 
    this.View.SetNeedsDisplay(); // Should force the View to redraw 
} 
+0

谢谢CHIFFRE为您的答案。我可以使用断点调试代码,并添加了控制台语句,它们都按预期工作,但即使在添加SetNeedsDisplay()后,背景颜色也不会改变。我已经在Lion OS上安装了最新版本的MonoTouch(稳定)版本。 – skjagini 2012-03-17 07:12:31

0

这个例子是从MonoTouch的书籍之一采取了,我不得不转换的源代码,对MonoTouch的最新版本一起使用。

原始源所指子视图作为

this.subView.BackgroundColor = UIColor.LightGray; 

在这个过程中我无法创建为视图的出口,即子视图。

如果没有创建出口,所述视图可以以下列方式也

this.View.SubViews[0].BackgroundColor = UIColor.LightGray 

访问和它解决了这一问题。