从选择器中的不同 class 调用函数

call a func from different class in selector

我正在使用 UIGestureRecognizer。我试图在选择器中从不同的 class 调用一个函数,但是当它执行时我得到 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")]

}

您设置的一切都正确,但是在初始化 helpClasses 时犯了一个错误。

因为您在 addTaped() 函数的作用域内声明了 helpClasses 变量,它将分配在堆栈上。一旦你的函数完成,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!
}

要从另一个 class 调用选择器方法,您只需执行以下操作:

let recognizer = UISwipeGestureRecognizer(target: objClass,
                                          action: #selector(objClass.actionMethodName))