Swift: 从两个不同的按钮调用相同的方法
Swift: Calling the same method from two different buttons
当我点击屏幕左角的 orangeButtonOne 时,我的 collectionView 出现,当我再次点击 orangeButtonOne 时,collectionView 消失。这工作正常,但现在......如您所见,collectionView 也包含很多按钮,当我按下其中一个按钮时,它会调用与 orangeButtonOne 完全相同的方法,即 closeDropDownView。但是,当我点击 collectionView 中的一个按钮时,应用程序在第 99 行崩溃。我收到以下错误:
class TestViewController: UIViewController {
@IBOutlet weak var dropDownView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
dropDownView.isHidden = true
func closeDropDownView() {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveLinear, animations: {
99 var dropTopFrame = self.dropDownView.frame <THREAD1: FATAL ERROR: UNEXPECTEDLY FOUND NIL WHILE IMPLICITLY UNWRAPPING AN OPTIONAL VALUE
var dropBottomFrame = self.dropDownView.frame
dropTopFrame.origin.y += dropBottomFrame.size.height
dropBottomFrame.origin.y -= dropTopFrame.size.height
self.dropDownView.frame = dropTopFrame
self.dropDownView.frame = dropBottomFrame
UIView.animate(withDuration: 0.5) {
self.dimView.alpha = 0
}
}, completion: { finished in
self.dropDownView.isHidden = true
print("dropView closed!")
})
}
}
我不明白这个方法在 orangeButtonOne 调用时如何正常工作,但是当 collectionView 按钮调用此方法时,帧值突然为零?
下面是每个按钮如何调用 closeDropDownView 方法:
class TestViewController: UIViewController {
//viewDidLoad etc
@IBAction func orangeButtonOneTapped(_ sender: Any) {
if (dropDownView.isHidden == true ) {
openDropDownView()
}
else { closeDropDownView() }
}
extension DropDownViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
let testVC = TestViewController()
@objc func CVButtonTapped(sender: UIButton!) {
print("button tapped")
testVC.closeDropDownView()
}
}
您正在崩溃,因为您正在 DropDownViewController
中创建 TestViewController
的新实例并在该特定实例 (testVC
) 上调用 closeDropDownView
方法。在 TestViewController
的新实例中,刚初始化时 @IBOutlet var dropDownView
是空的,因此会发生崩溃。为避免崩溃,您需要将 TestViewController
的相同实例传递给 DropDownViewController
以调用完全相同的 closeDropDownView
实例以获得相同的功能而不是崩溃。
当我点击屏幕左角的 orangeButtonOne 时,我的 collectionView 出现,当我再次点击 orangeButtonOne 时,collectionView 消失。这工作正常,但现在......如您所见,collectionView 也包含很多按钮,当我按下其中一个按钮时,它会调用与 orangeButtonOne 完全相同的方法,即 closeDropDownView。但是,当我点击 collectionView 中的一个按钮时,应用程序在第 99 行崩溃。我收到以下错误:
class TestViewController: UIViewController {
@IBOutlet weak var dropDownView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
dropDownView.isHidden = true
func closeDropDownView() {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveLinear, animations: {
99 var dropTopFrame = self.dropDownView.frame <THREAD1: FATAL ERROR: UNEXPECTEDLY FOUND NIL WHILE IMPLICITLY UNWRAPPING AN OPTIONAL VALUE
var dropBottomFrame = self.dropDownView.frame
dropTopFrame.origin.y += dropBottomFrame.size.height
dropBottomFrame.origin.y -= dropTopFrame.size.height
self.dropDownView.frame = dropTopFrame
self.dropDownView.frame = dropBottomFrame
UIView.animate(withDuration: 0.5) {
self.dimView.alpha = 0
}
}, completion: { finished in
self.dropDownView.isHidden = true
print("dropView closed!")
})
}
}
我不明白这个方法在 orangeButtonOne 调用时如何正常工作,但是当 collectionView 按钮调用此方法时,帧值突然为零?
下面是每个按钮如何调用 closeDropDownView 方法:
class TestViewController: UIViewController {
//viewDidLoad etc
@IBAction func orangeButtonOneTapped(_ sender: Any) {
if (dropDownView.isHidden == true ) {
openDropDownView()
}
else { closeDropDownView() }
}
extension DropDownViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
let testVC = TestViewController()
@objc func CVButtonTapped(sender: UIButton!) {
print("button tapped")
testVC.closeDropDownView()
}
}
您正在崩溃,因为您正在 DropDownViewController
中创建 TestViewController
的新实例并在该特定实例 (testVC
) 上调用 closeDropDownView
方法。在 TestViewController
的新实例中,刚初始化时 @IBOutlet var dropDownView
是空的,因此会发生崩溃。为避免崩溃,您需要将 TestViewController
的相同实例传递给 DropDownViewController
以调用完全相同的 closeDropDownView
实例以获得相同的功能而不是崩溃。