Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自定义抽头识别器方法赢得’;如果在其他类中实现,则无法工作 是否可以创建任何其他类都可以访问的全局方法来添加tap识别器?_Ios_Swift_Uilabel_Global_Uitapgesturerecognizer - Fatal编程技术网

Ios 自定义抽头识别器方法赢得’;如果在其他类中实现,则无法工作 是否可以创建任何其他类都可以访问的全局方法来添加tap识别器?

Ios 自定义抽头识别器方法赢得’;如果在其他类中实现,则无法工作 是否可以创建任何其他类都可以访问的全局方法来添加tap识别器?,ios,swift,uilabel,global,uitapgesturerecognizer,Ios,Swift,Uilabel,Global,Uitapgesturerecognizer,(我是swift的初学者) 背景/我想要实现的目标: 由于我将在各种视图中使用大量的点击识别器(用于标签和图像视图),因此我希望创建一个默认方法,在每次需要点击识别器时帮助我保存几行代码 ->我想要的例子 class Toolbox { static func customTapRecognizerForLabel () {} static func customTapRecognizerForImage () {} } 因此,我可以在不同的视图控制器中使用它们: class ViewCo

(我是swift的初学者)

背景/我想要实现的目标:

由于我将在各种视图中使用大量的点击识别器(用于标签和图像视图),因此我希望创建一个默认方法,在每次需要点击识别器时帮助我保存几行代码

->我想要的例子

class Toolbox {
 static func customTapRecognizerForLabel () {}
 static func customTapRecognizerForImage () {}
}
因此,我可以在不同的视图控制器中使用它们:

class ViewControllerOne{
 Toolbox.customTapRecognizerForLabel()
}
class ViewControllerTwo{
 Toolbox.customTapRecognizerForLabel()
}

到目前为止我做了什么?:

什么不起作用: 我在Toolbox.swift中创建了Toolbox类,试图在其他视图控制器中调用它,但它不起作用。(还尝试定义类的共享实例,而不是使用静态方法,但也不起作用)

什么有效: 在同一视图控制器中实现这些方法

我的工具箱代码:

import Foundation
import UIKit

class Toolbox: UIViewController {

static func tapRecognizerforLabel (named label: UILabel, action: Selector) {
 let tapGestureForLabel = UITapGestureRecognizer(target: self, action: action)
 label.addGestureRecognizer(tapGestureForLabel)
 label.isUserInteractionEnabled = true
 }
}
我如何称呼它:

class ViewOne: UITableViewCell {

@IBOutlet weak var nameLabel: UILabel!

override func awakeFromNib() {
 super.awakeFromNib()
 Toolbox.tapRecognizerforLabel(named: nameLabel, action: #selector(self.methodAlpha))
}

func methodAlpha() {
 print("It's friday my dudes")
}
}
触摸标签时出现的错误:

2018-07-13 11:08:22.131602-0500 MyApp[20435:1274296] 
+[MyApp.Toolbox methodAlpha]: unrecognized selector 
sent to class 0x1033c0038

2018-07-13 11:08:22.218289-0500 MyApp[20435:1274296] 
*** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: 
'+[MyApp.Toolbox methodAlpha]: unrecognized selector 
sent to class 0x1033c0038'

如果我在ViewOne类中实现tapRecognizerforLabel()方法,为什么它可以工作,而如果我在其他类中实现它,为什么它不能工作?

欢迎提出建议,以其他方式实现我的目标


谢谢:)

您需要像这样发送目标

func tapRecognizerforLabel (named label: UILabel, action: Selector,target:Any) { 
  let tapGestureForLabel = UITapGestureRecognizer(target:target, action: action)
  label.addGestureRecognizer(tapGestureForLabel)
  label.isUserInteractionEnabled = true
} 
目标应该包含选择器中方法的实现,因为您将
self
设置为
Toolbox
,而它不包含它,因此会发生崩溃

Toolbox.tapRecognizerforLabel(named: nameLabel, action: #selector(self.methodAlpha),target:self)