信息
我正在使用 Xamarin Studio 和 Xcode。
我的两个按钮“IncreaseButton”和“DecreaseButton”都将其发送的事件“TouchUpInside”附加到我的 IBAction“buttonClick”。
下面的代码会在 partial 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();
}
}
}}
最佳答案
每个 View (包括 UIButton)作为一个整数标记属性,您可以设置以区分多个 View 。如果您只想为 Button 使用单个事件处理程序,则可以使用 Tag 属性。
IncreaseButton.Tag = 1;
DecreaseButton.Tag = -1;
partial void ButtonClick(NSObject sender)
{
clickCount = clickCount + ((UIButton)sender).Tag;
this.CountLabel.Text = clickCount.ToString();
}
关于c# - 如何检索按钮 (MonoTouch) 上的 TouchUpInside 返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19850312/