我制作了一个包含 4 个单元格的 Collection View ,当您按下单元格 1 中的按钮(我还没有制作其他单元格)时,它将带您到一个名为“FirstCollectionViewController”的新 ViewController。我有一个 pangesturderecognizer,当您在 Collection View 中滑动时,它会显示滑出菜单。
但是当你在“FirstCollectionViewController”中并且你想回到 Collection View 时,你可以按下左上角的按钮。但是当我按下它时,我会在 CollectionViewController 的 ViewDidLoad 中的“self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())”处收到 EXC_BAD_INSTRUCTION 错误。我试图将 panGestureRecognizer 放在 ViewDidAppear 中,但同样发生了
我该如何解决?
顺便说一句,应该将您送回 Collection View 的 segue 称为:“SegueFirstCollectionViewController”
Collection View Controller :
import Foundation
import UIKit
class CollectionViewController: UICollectionViewController {
var Array = [String]()
var ButtonArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
Array = ["1","2","3","4"]
ButtonArray = ["1", "2", "3", "4"]
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
override func viewDidAppear(animated: Bool) {
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return Array.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell
let Label = cell.viewWithTag(1) as! UILabel
Label.text = Array[indexPath.row]
let Button = cell.viewWithTag(2) as! UIButton
Button.setTitle(ButtonArray[indexPath.row], forState: UIControlState.Normal)
// Button.addTarget(self,action:#selector(ButtonArray(_:)), forControlEvents:.TouchUpInside)
Button.addTarget(self, action: Selector("ButtonArray:"), forControlEvents:.TouchUpInside)
return cell
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Inside didSelectItemAtIndexPath")
}
func ButtonArray(sender : UIButton) {
print("m")
if sender.currentTitle == "1"{
performSegueWithIdentifier("segueFirst", sender: nil)
}
if sender.currentTitle == "2"{
}
if sender.currentTitle == "3"{
}
if sender.currentTitle == "4"{
}
}
}
第一个 Collection View Controller :
class FirstCollectionViewController : UIViewController {
@IBAction func SegueFirstCollectionViewController(sender: UIButton) {
performSegueWithIdentifier("SegueFirstCollectionViewController", sender: nil)
}
}
最佳答案
你误用了 segues。 Segues 创建其目标 ViewController 的新实例,这意味着当您启动您的应用程序时,您有 1 个 CollectionViewController,然后您单击一个单元格,您有一堆 1 个 CollectionViewController 和 1 个 FirstViewController,然后您点击按钮,如果看起来像这样,您不会回到原来的 CollectionViewController,而是创建一个新的 CollectionViewController,这意味着您现在有 1 个 collectionViewController、一个第一个 viewController 和一个 CollectionViewController。
这就是你重新执行 viewDidLoad 的原因,这就是它失败的原因,因为平移手势识别器已经被添加到另一个 View 中....
如果你想实现扁平化(push pop/master -> detail/back button...)你应该将你的 collectionViewController 封装在 NavigationViewController 中并使用自由给定的后退按钮:不要自己处理后退按钮, 特别是 SEGUE
PS 为了准确起见,但为了广告公众:存在一种特殊的 segue 来处理后面的问题,它们称为 unwind segue。但是,UINavigationController 应该始终是平面导航的首选选项。 Unwind segue 在处理垂直导航(模态显示/确定 - 取消)时找到它们的用处
关于ios - 由于 PanGesture,快速获取 EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38956729/