我正在尝试从我的UIVmageController扩展方法在我的UIImageView上添加一个手势识别器。
点击图像时我要触发的方法是在我的swift扩展中声明的类方法:
class func openPopViewWithText(text: String!) {
print("fire!")
}
我也从我的扩展类中添加了选择器。这是我添加选择器的方式:
infoImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(UIViewController.openPopViewWithText(_:)) ))
这是我点击ImageView时出现的错误:
[XXXDisplayStatsViewController openPopViewWithText:]: unrecognized selector sent to instance 0x7fd31520ada0
我的扩展程序类中的完整代码:
public extension UIViewController {
func addInfoImageViewWithText(infoText: String){
let margins : CGFloat = 30.0
let infoImageView : UIImageView = UIImageView(image: UIImage(named: "info"));
let yOrigin : CGFloat = ((self.view.y + self.view.height) - infoImageView.height) - margins
infoImageView.frame = CGRectMake(margins, yOrigin, infoImageView.width + 5, infoImageView.height + 5)
self.view.insertSubview(infoImageView, atIndex: 0)
self.view.bringSubviewToFront(infoImageView)
infoImageView.userInteractionEnabled = true
infoImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(UIViewController.openPopViewWithText(_:)) ))
}
class func openPopViewWithText(text: String!) {
print("fire!")
}
}
答案 0 :(得分:1)
问题是,a[i:] for i in range(3)
[0, 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[2, 3, 4, 5]
a[:i] for i in range(3)
[]
[0]
[0, 1]
a[-i:] for i in range(3)
[0, 1, 2, 3, 4, 5]
[5]
[4, 5]
a[:i] for i in [None, -1, -2] DESIRED RESULT
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
a[:-i] for i in range(3) BUT CAN'T GET IT HERE
[]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
a[:i] for i in [0, -1, -2] OR HERE EITHER
[]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
函数中的第一个参数是openPopViewWithText
而不是String
。点击UITapGestureRecognizer
手势识别器时会触发,它会将手势识别器实例传递给选择器,以便您可以跟踪其属性。所以你应该做的是将当前的函数签名更改为:
UIImageView
除此之外,您不应该使用类功能,因为您无法访问任何class func openPopViewWithText(recognizer: UITapGestureRecognizer) {
print("fire!")
}
属性。