2016-08-04 4 views
0

私は、 "1"/ButtonArray.index[0]というボタンを押すと、segue(segue識別子は "segueFirst")を実行し、別のUIViewController "FirstCollectionViewController " ボタンを押しても何も起こりません。私のセグやボタンが素早く動かない

今、私はそれがセグーと関係があるかどうかわかりませんが、それは私が疑うところです。私はそれがボタンと関係があると思うが、私はそれを理解することはできない。

PS:ButtonArray関数(セグをトリガするはずです)の中で、私はperformSegueWithIdentifierを試しましたが、ボタンを押しても何も起こりません。

私もpresentViewControllerを試してみましたが、それは

私は、このエラーを与えることは予想される引数の型「のUIViewController」私は理解していない

に「FirstCollectionViewController.Type」型の値を変換できません。 ViewController(「新しいVC」では)を宣言しているためです。

VC1:

 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"] 


      } 

      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: "ButtonArray", forControlEvents:.TouchUpInside) 
       return cell 

      } 

      func ButtonArray(sender : UIButton) { 
      print("Not the function") 
       var selectedButtonCell = sender.superview as! UICollectionViewCell 

       var indexPath = collectionView?.indexPathForCell(selectedButtonCell) 
       if indexPath?.row == 0 { 
print("Not the if-statement") 
        // presentViewController(FirstCollectionViewController, animated: true, completion: nil) 
        // performSegueWithIdentifier("segueFirst", sender: nil) 
       } 
      } 
    } 

新しいVC:

import UIKit 
import Foundation 
    class FirstCollectionViewController : UIViewController { 

     override func viewDidLoad() { 
      print("hey") 
     } 
    } 

答えて

0

あなたのcellForItemAtIndexPathの内側にあなたのボタンでターゲットを追加するのを忘れ、Button

のご setTitleメソッドの後に次の行を追加します。
Button.addTarget(self,action: Selector("ButtonArray:"), forControlEvents: .TouchUpInside) 

私は問題があなたのボタンフレームが小さすぎるかもしれないと思うので、didSelectItemAtIndexPathメソッドのUICollectionViewDelegate、またあなたのViewControllerを使ってあなたのcollectionViewデリゲートをセットしなければならない場合は、didSelectItemAtIndexPathを呼び出しているかどうかチェックしてください。

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    print("Inside didSelectItemAtIndexPath") 
} 
+0

さて、私はあなたが要求したが、私はそれを変えたので、私は「期待区切り」のようないくつかのエラーを得たものを試してみましたが「アクション: 『ButtonArray』アクション」の代わりに」:#selector(ButtonArray(_ :)) "しかし、それは私がそれを押したときにアクションを引き起こさないということです。ボタン、ButtonArray関数、またはifステートメントが配置されているかどうかを調べるために2つのprintステートメントを配置しました。ボタンまたはButtonArray関数ですが、私はそれを修正する方法がわかりません:( –

+0

'selector'構文と他の詳細の編集を確認してください –

+0

私は 'func ButtonArray内のすべてのコード行をコメントアウトしました'' 'var selectedButtonCell = sender.superview as SICABRTエラー!(!UICollectionViewCell'という名前でSIGABRTエラーが発生したため)、ButtonArray関数内にステートメントが出力されます。ボタンは機能しています! "didSelectItemAtIndexPathの中身"が印刷されませんでしたが、私の主な問題はあなたのおかげで解決しました! –

関連する問題