在iOS中触发按钮单击事件两次或更多次

时间:2015-08-24 10:19:59

标签: ios xamarin click

我在一个部分添加了一个按钮并编写了点击的事件

var refreshCacheSettings = new CustomButtonSection ("GeneralSettingsView_RefreshCache_Button_Title".t());

            refreshCacheSettings.ButtonClicked += async (sender, e) => {

                Console.WriteLine ("Clicked...");
                var btn = (UIButton)sender;
                btn.Enabled=false;
                btn.UserInteractionEnabled =false;

                var answer= await AlertUtil.CreateMessageYesNo("GeneralSettingsView_RefreshCache_Question_Title".t(),"GeneralSettingsView_RefreshCache_Question_Message".t());

                if(!answer)
                {
                    ShowLoadingScreen();
                    await Task.Run(()=> RefreshFormFieldSettings());
                }


                btn.Enabled=true;
                btn.UserInteractionEnabled =true;

            };

这是自定义按钮部分类

public class CustomButtonSection : RootElement
{
    string _btnText;
    public event EventHandler ButtonClicked;
    public CustomButtonSection (string btnText):base ("")
    {
        base.MakeViewController ();
        _btnText = btnText;

    }


    protected override UIViewController MakeViewController ()
    {
        var vc = (UITableViewController) base.MakeViewController();

        vc.TableView.BackgroundView = null;
        vc.View.BackgroundColor = UIColor.White; //or whatever color you like
        return vc;
    }

    public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
    {
        base.Deselected (dvc, tableView, path);
    }

    public override UITableViewCell GetCell (UITableView tv)
    {

        var cell = tv.DequeueReusableCell ("CustomCell") as CustomButtonCell;
        if (cell == null)
            cell = new CustomButtonCell (new NSString("CustomCell"));

        cell.Accessory = UITableViewCellAccessory.None;
        cell.SelectionStyle = UITableViewCellSelectionStyle.None;
        cell.UpdateCell (_btnText);
        cell.btn.TouchUpInside += (sender, e) => {
            if (ButtonClicked != null) 
                ButtonClicked (sender, e);

        };
        return cell;

    } 


    private class CustomButtonCell: UITableViewCell
    {
        public UIButton btn;


        public CustomButtonCell(NSString cellId )
            : base(UITableViewCellStyle.Default, cellId)
        {
            SelectionStyle = UITableViewCellSelectionStyle.None;
            Accessory = UITableViewCellAccessory.DisclosureIndicator;
            ContentView.BackgroundColor = UIColor.White;
            btn = ImagewareMobile.iOS.UI.Common.Buttons.ElementsButton("", FitpulseTheme.SharedTheme.BlueButtonImage);


        ContentView.AddSubviews(new UIView[] {btn});

        }

        public void UpdateCell (string caption )
        {
            btn.SetTitle(caption,UIControlState.Normal);

        }

        public override void LayoutSubviews ()
        {
            base.LayoutSubviews ();
            btn.SetTitleColor(UIColor.White, UIControlState.Normal);
            btn.Frame = new CGRect (5,5,300,35);

        }
    }

}

有时我收到两次或更多次警报信息。它不会一直发生,但有时会发生,而且很无聊......

在javascript中有preventDefault方法,但是对于ios呢?

我在c#中使用xamarin.ios,但我可以处理目标C或swift的代码。

1 个答案:

答案 0 :(得分:3)

此行为的原因是每次重复使用单元格时都会应用该事件。

public override UITableViewCell GetCell (UITableView tv)
{

    var cell = tv.DequeueReusableCell ("CustomCell") as CustomButtonCell;
    if (cell == null)
        cell = new CustomButtonCell (new NSString("CustomCell"));

    cell.Accessory = UITableViewCellAccessory.None;
    cell.SelectionStyle = UITableViewCellSelectionStyle.None;
    cell.UpdateCell (_btnText);

    // this is the reason for multiple events fired
    cell.btn.TouchUpInside += (sender, e) => {
        if (ButtonClicked != null) 
            ButtonClicked (sender, e);

    };
    return cell;

}

最好将代码更改为

public override UITableViewCell GetCell (UITableView tv)
{

    var cell = tv.DequeueReusableCell ("CustomCell") as CustomButtonCell;
    if (cell == null)
    {
        cell = new CustomButtonCell (new NSString("CustomCell"));
        cell.btn.TouchUpInside += (sender, e) => {
        if (ButtonClicked != null) 
            ButtonClicked (sender, e);

        };
    }
    cell.Accessory = UITableViewCellAccessory.None;
    cell.SelectionStyle = UITableViewCellSelectionStyle.None;
    cell.UpdateCell (_btnText);

    return cell;

}

这样,只有在新创建单元格时才会附加事件。