简体   繁体   English

在#selector Swift 2.2中传递参数

[英]Pass on Argument in #selector Swift 2.2

I have a method: 我有一个方法:

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

And also I have a method: 而且我有一种方法:

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

And it's work all right. 一切正常。 But I need to pass on channelId to followIconTapped() method. 但是我需要将channelId传递给followIconTapped()方法。

I try this: 我尝试这样:

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

and then I try catch it: 然后我尝试抓住它:

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

xCode say that channelId will never used. xCode表示channelId永远不会使用。 Why? 为什么? When I build project I haven't any issue. 当我构建项目时,我没有任何问题。 But app is crash if I tap on followIcon . 但是,如果我点击followIcon,应用程序将崩溃。

Please, can you give me advice how to pass on channelId to followIconTapped() 请给我建议如何将channelId传递给followIconTapped()

creating a generic UITapGestureRecognizer instead use this: 创建通用的UITapGestureRecognizer而不是使用以下命令:

class CustomTapGestureRecognizer: UITapGestureRecognizer {
    var channelId: String?
}

also use this: 也使用这个:

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
    }
}

尝试为tapGestureRecognizer设置标签,并根据标签定义channelId

You cannot pass extra arguments to your method when you are using Selector . 使用Selector时,不能将额外的参数传递给方法。 UITapGestureRecognizer can handle methods with signatures: UITapGestureRecognizer可以处理带有签名的方法:

  • private dynamic func handleTap() { ... }
  • private dynamic func handleTapOn(gestureRecognizer: UIGestureRecognizer) { ... }

Selectors in swift aren't validate in other way then check syntactics. 快速选择器不会以其他方式进行验证,然后检查语法。 Thats why there is no error connected with method signature you pass to action method. 这就是为什么传递给操作方法的方法签名没有错误的原因。

If you like use channelId in your handleTap method you have to keep your variable in some place. 如果您喜欢在handleTap方法中使用channelId ,则必须将变量保留在某个位置。 You can create property in your class and store there your variable to use in handleTap method. 您可以在类中创建属性,然后在其中存储要在handleTap方法中使用的变量。

class YourClass: BaseClass {

    // MARK: - Properties

    private var channellId: String? = nil 

    // MARK: - API

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

    // MARK: - Callbacks

    private dynamic func handleTapOn(recognizer: UIGestureRecognizer) {
        if let channelId = self.channelId {
            // Do sth
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM