如果目标是正常的,UITapGestureRecognizer 无法正常工作 class
UITapGestureRecognizer is not working properly if the target is a normal class
我有一个简单的 ViewController,中间只有一个视图,当我添加 UITapGestureRecognizer 时它会正常工作,但是当我从外部尝试同样的事情时 class 它不会工作(从未调用该方法)。
我的问题是
我需要对我的 Listener class 进行哪些更改才能使其调用其中的方法
代码如下:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
Listener(view : view , viewController: self).listen()
}
@objc func onNewTabArrived(){
print("Hello from inside the controller")
}
}
class Listener {
let view : UIView
let viewController: UIViewController
init(view : UIView , viewController: UIViewController){
self.view = view
self.viewController = viewController
}
func listen(){
// If you set the target as viewController it will call the method named onNewTabArrived inside it , printing "Hello from inside the controller"
// let tab = UITapGestureRecognizer(target: viewController, action: #selector(onNewTabArrived))
// view.addGestureRecognizer(tab)
// This will not work! the text "Hello from inside Listener" will never be printed, How can I force it to be called ?.
let tab = UITapGestureRecognizer(target: self, action: #selector(onNewTabArrived))
view.addGestureRecognizer(tab)
}
@objc func onNewTabArrived(){
print("Hello from inside Listener")
}
}
您的 Listener
对象在您创建后立即被释放。尝试将其存储到 属性
@IBOutlet weak var myView: UIView!
var listener: Listener?
override func viewDidLoad() {
super.viewDidLoad()
self.listener = Listener(view : view , viewController: self)
self.listener?.listen()
}
我有一个简单的 ViewController,中间只有一个视图,当我添加 UITapGestureRecognizer 时它会正常工作,但是当我从外部尝试同样的事情时 class 它不会工作(从未调用该方法)。 我的问题是
我需要对我的 Listener class 进行哪些更改才能使其调用其中的方法
代码如下:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
Listener(view : view , viewController: self).listen()
}
@objc func onNewTabArrived(){
print("Hello from inside the controller")
}
}
class Listener {
let view : UIView
let viewController: UIViewController
init(view : UIView , viewController: UIViewController){
self.view = view
self.viewController = viewController
}
func listen(){
// If you set the target as viewController it will call the method named onNewTabArrived inside it , printing "Hello from inside the controller"
// let tab = UITapGestureRecognizer(target: viewController, action: #selector(onNewTabArrived))
// view.addGestureRecognizer(tab)
// This will not work! the text "Hello from inside Listener" will never be printed, How can I force it to be called ?.
let tab = UITapGestureRecognizer(target: self, action: #selector(onNewTabArrived))
view.addGestureRecognizer(tab)
}
@objc func onNewTabArrived(){
print("Hello from inside Listener")
}
}
您的 Listener
对象在您创建后立即被释放。尝试将其存储到 属性
@IBOutlet weak var myView: UIView!
var listener: Listener?
override func viewDidLoad() {
super.viewDidLoad()
self.listener = Listener(view : view , viewController: self)
self.listener?.listen()
}