2017-04-13 20 views
1

我正在使用UIGestureRecognizer。我試圖在選擇器中調用不同類的func,但執行時我得到了NSInvalidArgumentException在選擇器中調用不同類中的func

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")] 

} 
+1

你可以創建一個最小的代碼示例,所以我們沒有通過所有的代碼來挖? –

回答

2

你是對建立的一切,但要一個錯誤,當你初始化你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! 
} 
+0

Thansks的答案。這是真的 :) –

相關問題