gpt4 book ai didi

swift - 如何将 View 作为参数传递(快速)

转载 作者:行者123 更新时间:2023-11-30 11:40:34 25 4
gpt4 key购买 nike

我正在尝试将功能齐全的滑动手势代码移至view model清理view controller但代码使用了很多selfview引用文献,所以我想我需要传递 viewUIView.self作为调用函数时的参数。但无法让它工作。尝试过:

vm.swipeCode(myView: self.view)

func swipeCode(myView: UIView) {...

但是它崩溃了。经过一番研究后,我还尝试了 inout 的变体和&但无济于事。这是完整的刷卡代码(它引用回view controller,但当事情开始工作时我也会移动它们:))

var myVC = RecipesViewController()

func swipeCode(myView: UIView) {
//SWIPE RIGHT
let swipingRight = UISwipeGestureRecognizer()
swipingRight.addTarget(self, action: #selector(myVC.swipeRight))
swipingRight.direction = .right
swipingRight.delegate = self as? UIGestureRecognizerDelegate
swipingRight.cancelsTouchesInView = false

myView.isUserInteractionEnabled = true
myView.addGestureRecognizer(swipingRight)

//// ALLOW SWIPE LEFT ////
let swipingLeft = UISwipeGestureRecognizer()
swipingLeft.addTarget(self, action: #selector(myVC.swipeLeft))
swipingLeft.direction = .left
swipingLeft.delegate = self as? UIGestureRecognizerDelegate
swipingLeft.cancelsTouchesInView = false

myView.isUserInteractionEnabled = true
myView.addGestureRecognizer(swipingLeft)
}

最佳答案

崩溃可能是因为这两行:

swipingRight.addTarget(self, action: #selector(myVC.swipeRight))
swipingLeft.addTarget(self, action: #selector(myVC.swipeLeft))

您将 myVC.swipeLeft 作为操作传递,但将 self 作为目标,因此手势识别器将尝试查找 swipeLeft 方法在 self 中,它不存在。

您应该始终确保该操作是目标的成员:

swipingRight.addTarget(myVC, action: #selector(myVC.swipeRight))
swipingLeft.addTarget(myVC, action: #selector(myVC.swipeLeft))

关于swift - 如何将 View 作为参数传递(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49333841/

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