gpt4 book ai didi

ios - 在#selector Swift 2.2 中传递参数

转载 作者:行者123 更新时间:2023-11-28 18:29:05 25 4
gpt4 key购买 nike

我有一个方法:

func followUnfollow(followIcon: UIImageView, channelId: String) {
let followUnfollow = followIcon
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.followIconTapped(_:)))
followUnfollow.userInteractionEnabled = true
followUnfollow.addGestureRecognizer(tapGestureRecognizer)
}

我还有一个方法:

func followIconTapped(sender: UITapGestureRecognizer) {
...
}

而且一切正常。但我需要将 channelId 传递给 followIconTapped() 方法。

我试试这个:

func followUnfollow(followIcon: UIImageView, channelId: String) {
...
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.followIconTapped(_:channelId)))
...
}

然后我试着捕获它:

func followIconTapped(sender: UITapGestureRecognizer, channelId: String) {
...
}

xCode 说 channelId 永远不会被使用。为什么? 当我构建项目时,我没有任何问题。但是,如果我点击 followIcon,应用程序会崩溃。

请给我一些建议,如何将 channelId 传递给 followIconTapped()

最佳答案

创建一个通用的 UITapGestureRecognizer 而不是使用这个:

class CustomTapGestureRecognizer: UITapGestureRecognizer {
var channelId: String?
}

也用这个:

override func viewDidLoad() {
super.viewDidLoad()

let gestureRecognizer = CustomTapGestureRecognizer(target: self, action: #selector(tapped(_:))
gestureRecognizer.channelId = "Your string"
view1.addGestureRecognizer(gestureRecognizer)
}

func tapped(gestureRecognizer: CustomTapGestureRecognizer) {
if let channelId = gestureRecognizer.channelId {
//print
}
}

关于ios - 在#selector Swift 2.2 中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38919140/

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