我正在使用UIGestureRecognizer
。我试图在选择器中调用来自不同类的func,但是当它执行时我得到NSInvalidArgumentException
。
import Foundation
import UIKit
class helperClass {
var onBoardingImageArray : [UIImage]?
var onBoardingPageControl : UIPageControl?
var onBoardingImageView : UIImageView?
init(imageArray : [UIImage] , pageControl : UIPageControl , yourImageView : UIImageView) {
onBoardingImageArray = imageArray
onBoardingPageControl = pageControl
onBoardingImageView = yourImageView
}
@objc func firstImageSwipeGestureAction(gesture :UIGestureRecognizer){
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
if (onBoardingPageControl?.currentPage)! > 0{
print("Swiped right")
onBoardingPageControl?.currentPage -= 1
self.onBoardingImageView?.image = onBoardingImageArray?[(onBoardingPageControl?.currentPage)!]
}
case UISwipeGestureRecognizerDirection.left:
if (onBoardingPageControl?.currentPage)! < (onBoardingImageArray?.count)! - 1{
print("Swiped left")
onBoardingPageControl?.currentPage += 1
self.onBoardingImageView?.image = onBoardingImageArray?[(onBoardingPageControl?.currentPage)!]
}
default:
break
}
}
}
}
import UIKit
class MainController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.addTaped()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addTaped(){
let helpClasses : helperClass = helperClass.init(imageArray: self.firtImageViewArray! , pageControl:firstPageControl , yourImageView: firstImageView)
let firstImageswipeGestureRecognizer = UISwipeGestureRecognizer(target: helpClasses, action: #selector(helpClasses.firstImageSwipeGestureAction))
firstImageswipeGestureRecognizer.direction = .right
self.firstImageView.isUserInteractionEnabled = true
self.firstImageView.addGestureRecognizer(firstImageswipeGestureRecognizer)
let firstImageswipeGestureRecognizerLeft = UISwipeGestureRecognizer(target: helpClasses, action: #selector(helpClasses.firstImageSwipeGestureAction))
firstImageswipeGestureRecognizer.direction = .left
self.firstImageView.isUserInteractionEnabled = true
self.firstImageView.addGestureRecognizer(firstImageswipeGestureRecognizerLeft)
}
@IBOutlet weak var firstImageView: UIImageView!
@IBOutlet weak var firstPageControl: UIPageControl!
let firtImageViewArray : [UIImage]? = [#imageLiteral(resourceName: "Eagle9")]
}
答案 0 :(得分:4)
您正在设置一切正确但在初始化helpClasses
时犯了一个错误。
因为您在helpClasses
函数的范围内声明了addTaped()
变量,所以它将在堆栈中分配。函数完成后,helpClasses
变量将被释放,从堆栈中删除,变为nil
。从而开始,您向对象发送消息,nil
是什么,因此可以理解,没有任何反应。
要解决此问题,请在函数范围之外的堆上声明您的变量。如果您在MainController
的范围内声明它,那就最好。
示例:强>
import UIKit
class MainController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.addTaped()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addTaped(){
// Initialise helpclasses here, but not as a local variable!!
helpClasses = helperClass.init(imageArray: self.firtImageViewArray! , pageControl:firstPageControl , yourImageView: firstImageView)
let firstImageswipeGestureRecognizer = UISwipeGestureRecognizer(target: helpClasses, action: #selector(helpClasses.firstImageSwipeGestureAction))
firstImageswipeGestureRecognizer.direction = .right
self.firstImageView.isUserInteractionEnabled = true
self.firstImageView.addGestureRecognizer(firstImageswipeGestureRecognizer)
let firstImageswipeGestureRecognizerLeft = UISwipeGestureRecognizer(target: helpClasses, action: #selector(helpClasses.firstImageSwipeGestureAction))
firstImageswipeGestureRecognizer.direction = .left
self.firstImageView.isUserInteractionEnabled = true
self.firstImageView.addGestureRecognizer(firstImageswipeGestureRecognizerLeft)
}
@IBOutlet weak var firstImageView: UIImageView!
@IBOutlet weak var firstPageControl: UIPageControl!
let firtImageViewArray : [UIImage]? = [#imageLiteral(resourceName: "Eagle9")]
// Move your helpClasses variable here
var helpClasses: helperClass!
}
答案 1 :(得分:0)
要从另一个类调用选择器方法,只需执行以下操作:
let recognizer = UISwipeGestureRecognizer(target: objClass,
action: #selector(objClass.actionMethodName))