[英]swift : thread 1: exc_bad_instruction(code=exc_1386_invop,subcode=0x0)
[英]Getting EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0) in swift because of PanGesture
我已經創建了一個包含4個單元格的集合視圖,當您按單元格1中的按鈕時(我尚未創建其他單元格),它將帶您到一個名為“ FirstCollectionViewController”的新ViewController中。 我有一個pangesturderecognizer,當您在集合視圖中滑動時會顯示滑出菜單。
但是,當您位於“ FirstCollectionViewController”中並想要返回到集合視圖時,可以按左上角的一個按鈕。 但是,當我按下它時,我會在CollectionViewController的ViewDidLoad中的“ self.view.addGestureRecognizer(self.revealViewController()。panGestureRecognizer())”處收到EXC_BAD_INSTRUCTION錯誤。 我試圖將panGestureRecognizer放在ViewDidAppear中,但是發生了同樣的情況
我該如何解決?
順便說一句,應該將您帶回到集合視圖的segue稱為:“ SegueFirstCollectionViewController”
CollectionViewController:
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"{
}
}
}
FirstCollectionViewController:
class FirstCollectionViewController : UIViewController {
@IBAction func SegueFirstCollectionViewController(sender: UIButton) {
performSegueWithIdentifier("SegueFirstCollectionViewController", sender: nil)
}
}
你不需要把牢房帶到外面
在中設定值
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
//set value here
if self.currentTitle == "1" ){
//
}
else {
//...
}
}
並重新加載特定的單元格
let indexPath = IndexPath(item: i, section: 0)
self.tableView.reloadRows(at: [indexPath], with: .automatic)
希望能為您提供幫助:)
您濫用segues。 Segues創建其目的地ViewController的新實例 ,這意味着,當您啟動您的應用時,您有1個CollectionViewController,然后單擊一個單元格,然后有1個CollectionViewController和1個FirstViewController的堆棧,然后單擊按鈕,事件看起來像它,您不會回到原始的CollectionViewController,而是要創建一個新的CollectionViewController,這意味着您現在擁有1個collectionViewController,一個第一個viewController和一個CollectionViewController。
這就是為什么您要重新執行viewDidLoad的原因,這就是為什么它失敗的原因,因為平移手勢識別器已被添加到另一個視圖中。
如果要實現扁平化(按下彈出按鈕/主菜單->詳細信息/后退按鈕...),則應將collectionViewController封裝在NavigationViewController中,並使用免費提供的后退按鈕: 不要處理自己的后退,尤其要使用SEGUE
PS是為了精確起見,但對於有廣告的公眾來說:存在一種特殊的segue來應對,它們被稱為unwind segue。 但是,UINavigationController應該始終是平面導航的首選選項。 放寬搜索在垂直導航中的用途(模態演示/確定-取消)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.