简体   繁体   English

如何在Swift4中不使用Objective-C的情况下向标签添加动作

[英]How to add action to a label without using objective-c in Swift4

Objective-C functions Objective-C功能

@objc func handleTapHash(gestureRecognizer: UIGestureRecognizer) {

        NotificationCenter.default.post(name: NSNotification.Name("ViewerOfHash"), object: nil)



    }
@objc func ViewerOfHash() {

        performSegue(withIdentifier: "ViewerOfHash", sender: nil)

        PassDataController.addData(newData: "12300")
        MainVC.tof = 1

    }

Add action to label(hashTag.hashTag1) 将操作添加到标签(hashTag.hashTag1)

let gestureRecognizer1 = UITapGestureRecognizer(target: self, action: #selector(self.handleTapHash))
hashTag.hashTag1.addGestureRecognizer(gestureRecognizer1)

It works, but I don't want to use objective-c and NS functions. 它可以工作,但是我不想使用Objective-C和NS函数。

There is a label that send some data to another page in my code. 在我的代码中有一个标签可以将一些数据发送到另一个页面。

How can I change my code to Swift4 fully? 如何将我的代码完全更改为Swift4?

Unfortunately, @objc is required because you're using Selectors in target:action: . 不幸的是, @objc是必需的,因为您在target:action:使用Selectors The ObjC runtime is required in order to lookup the method implementation for a given Selector . 为了查询给定Selector的方法实现,需要ObjC运行时。

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

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