gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:45:58 24 4
gpt4 key购买 nike

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

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呢?

我将 xamarin.ios 与 c# 一起使用,但我可以处理 objective-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;

}

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

关于ios - 按钮单击事件在 iOS 中被触发两次或更多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32179883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com