Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按钮单击事件在iOS中被触发两次或两次以上_Ios_Xamarin_Click - Fatal编程技术网

按钮单击事件在iOS中被触发两次或两次以上

按钮单击事件在iOS中被触发两次或两次以上,ios,xamarin,click,Ios,Xamarin,Click,我在一个分区上添加了一个按钮,并编写了点击事件 var refreshCacheSettings = new CustomButtonSection ("GeneralSettingsView_RefreshCache_Button_Title".t()); refreshCacheSettings.ButtonClicked += async (sender, e) => { Console.WriteLine ("Clicke

我在一个分区上添加了一个按钮,并编写了点击事件

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中有默认的方法,但是对于ios呢


我正在将xamarin.ios与c#一起使用,但我可以处理目标c或swift的代码。

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

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;

}

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

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

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;

}

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

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

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;

}

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

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

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;

}

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

请确认在滚动时问题是否变得更严重,或者是否退出屏幕并返回?我感觉你正在注册你的按钮点击次数times@Webmonger,谢谢你的回复。我检查的事件始终为空,如果您在上面查看,我每次都会使用new(var refreshCacheSettings=new custombuttonnsection(“GeneralSettingsView\u RefreshCache\u button\u Title.t());)创建按钮。请确认当您滚动时问题是否变得更严重,或者您是否离开屏幕并返回?我感觉你正在注册你的按钮点击次数times@Webmonger,谢谢你的回复。我检查的事件始终为空,如果您在上面查看,我每次都会使用new(var refreshCacheSettings=new custombuttonnsection(“GeneralSettingsView\u RefreshCache\u button\u Title.t());)创建按钮。请确认当您滚动时问题是否变得更严重,或者您是否离开屏幕并返回?我感觉你正在注册你的按钮点击次数times@Webmonger,谢谢你的回复。我检查的事件始终为空,如果您在上面查看,我每次都会使用new(var refreshCacheSettings=new custombuttonnsection(“GeneralSettingsView\u RefreshCache\u button\u Title.t());)创建按钮。请确认当您滚动时问题是否变得更严重,或者您是否离开屏幕并返回?我感觉你正在注册你的按钮点击次数times@Webmonger,谢谢你的回复。我检查事件始终为空,如果您查看上面的内容,我每次都会使用new(var refreshCacheSettings=new custombuttonnsection(“GeneralSettingsView\u RefreshCache\u button\u Title.t());)创建按钮谢谢你的回答。问题似乎是用那个代码解决的。谢谢你的回答。问题似乎是用那个代码解决的。谢谢你的回答。问题似乎是用那个代码解决的。谢谢你的回答。问题似乎是用那个代码解决的。