gpt4 book ai didi

swift - 通过 UITapGestureRecognizer 选择器向函数发送参数参数

转载 作者:搜寻专家 更新时间:2023-10-30 22:02:00 25 4
gpt4 key购买 nike

我正在使用 TapGestureRecognizer 制作一个具有可变 View 的应用程序。当按下 View 时,我目前正在这样做

func addView(headline: String) {
// ...
let theHeadline = headline
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
// ....
}

但是在我的函数“handleTap”中,我想像这样给它一个额外的参数(而不仅仅是发送者)

func handleTap(sender: UITapGestureRecognizer? = nil, headline: String) {
}

我如何将特定标题(每个 View 都是唯一的)作为 handleTap 函数的参数发送?

最佳答案

与其创建通用的 UITapGestureRecognizer,不如将其子类化并为标题添加一个属性:

class MyTapGestureRecognizer: UITapGestureRecognizer {
var headline: String?
}

然后改用它:

override func viewDidLoad() {
super.viewDidLoad()

let gestureRecognizer = MyTapGestureRecognizer(target: self, action: "tapped:")
gestureRecognizer.headline = "Kilroy was here."
view1.addGestureRecognizer(gestureRecognizer)
}

func tapped(gestureRecognizer: MyTapGestureRecognizer) {
if let headline = gestureRecognizer.headline {
// Do fun stuff.
}
}

我试过了。效果很好。

关于swift - 通过 UITapGestureRecognizer 选择器向函数发送参数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35635595/

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