c# - 如何检索按钮 (MonoTouch) 上的 TouchUpInside 返回值?

标签 c# ios iphone xamarin.ios

信息

我正在使用 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/

相关文章:

c# - 根据其他列中的值更改 DataGridTextColumn 单元格上的字符串格式,并保持值可排序

C# 选择累加器不工作的重载方法

ios - 链接到 Xcode 上的 arm64 库时出现链接器错误

iphone - Objective-c 语法高亮

c# - 如果可能的异常列表太多,如何捕获特定异常?

c# - 下载时更新对话框表单

ios - 无需单击按钮即可将数据从一个文本字段传输到另一个文本字段

ios - 无法进行应用内购买

iphone - 选择 UITableView 的多行

iphone - [Xcode]如何根据类型统计NSArray