gpt4 book ai didi

ios - 如何在运行时创建动态未知数量的按钮?如何在运行时为所有按钮添加一个 Action 监听器?

转载 作者:行者123 更新时间:2023-11-28 21:08:18 24 4
gpt4 key购买 nike

我想要这样的构建界面。用户将输入要显示的按钮范围。我是 iOS 开发的新手。我不知道如何在运行时帮助完成这一切..我想在数据库中存储按下和未按下按钮的数量,我该如何实现这个任何解决方案。

提前致谢

最佳答案

Hi you can achieve this by using collectionView(is the efficient approach) or by adding the buttons
So while adding button just add a tag of button with +1 and when user click the button just fetch button tag and you will get which button is clicked.
Please find below sample

var prevBtn = 0

func addButton()

let button = UIButton()
button.tag = prevBtn.tag + 1
button.addTarget(self, action:#selector(handleButtonAction),
for:.touchUpInside)
prevBtn = button.tag
}

func handleButtonAction(button: UIButton){

print(button.tag)

}

Thanks

关于ios - 如何在运行时创建动态未知数量的按钮?如何在运行时为所有按钮添加一个 Action 监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44616700/

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