如何在按钮(MonoTouch)上检索TouchUpInside返回?

时间:2013-11-08 02:01:44

标签: c# ios iphone xamarin.ios

信息

我正在使用Xamarin Studio和Xcode。

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

以下代码将在部分void buttonClick函数中生成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();
        }
    }
}}

2 个答案:

答案 0 :(得分: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();       
}

答案 1 :(得分:1)

每个视图(包括UIButton)作为整数标记属性,您可以设置它们以区分多个视图。如果您只想为Button设置一个事件处理程序,则可以使用Tag属性。

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

partial void ButtonClick(NSObject sender)
{
  clickCount = clickCount + ((UIButton)sender).Tag;
  this.CountLabel.Text = clickCount.ToString();
}
相关问题