gpt4 book ai didi

swift - 从选择器中的不同类调用函数

转载 作者:可可西里 更新时间:2023-11-01 00:41:17 25 4
gpt4 key购买 nike

我正在使用 UIGestureRecognizer。我正在尝试从选择器中的不同类调用函数,但在执行时出现 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!
}

关于swift - 从选择器中的不同类调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43388026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com