1

信息

我正在使用 Xamarin Studio 和 Xcode。

我的两个按钮“IncreaseButton”和“DecreaseButton”都将它们发送的事件“TouchUpInside”附加到我的 IBAction 'buttonClick' 上。

下面的代码将在部分 void buttonClick 函数中生成 2 个错误;但是,我的问题是如何在实现我在下面的代码中实现的目标的同时不产生这 2 个错误(如果这有意义的话)。

谢谢。

using System; 
using System.Drawing; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit;

namespace Allah
{
public partial class AllahViewController : UIViewController
{
    protected int clickCount;

    public AllahViewController () : base ("AllahViewController", null)
    {
    }

    public override void DidReceiveMemoryWarning ()
    {
        // Releases the view if it doesn't have a superview.
        base.DidReceiveMemoryWarning ();

        // Release any cached data, images, etc that aren't in use.
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        this.IncreaseButton.TouchUpInside += (sender, e) => {
            this.clickCount++;
        };

        this.DecreaseButton.TouchUpInside += (sender, e) => {
            this.clickCount--;
        }; 

        // Perform any additional setup after loading the view, typically from a nib.
    }

    partial void buttonClick (NSObject sender)
    {
        if (this.IncreaseButton.TouchUpInside == true)
        {
            this.CountLabel.Text = clickCount.ToString();
        }

        if (this.DecreaseButton.TouchUpInside == true)
        {
            this.CountLabel.Text = clickCount.ToString();
        }
    }
}}
4

2 回答 2

1

你可以这样写:

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();

    // Perform any additional setup after loading the view, typically from a nib.
}

partial void decreaseButtonClick (NSObject sender)
{
    clickCount--;
    this.CountLabel.Text = clickCount.ToString();       
}

partial void increaseButtonClick (NSObject sender)
{
    clickCount++;
    this.CountLabel.Text = clickCount.ToString();       
}
于 2013-11-08T02:56:01.410 回答
1

每个 View(包括 UIButton)作为一个整数 Tag 属性,您可以将其设置为将多个 View 彼此区分开来。如果您只想为您的 Button 提供一个事件处理程序,您可以使用 Tag 属性。

IncreaseButton.Tag = 1;
DecreaseButton.Tag = -1;

partial void ButtonClick(NSObject sender)
{
  clickCount = clickCount + ((UIButton)sender).Tag;
  this.CountLabel.Text = clickCount.ToString();
}
于 2013-11-08T14:24:16.360 回答