2017-01-11 10 views
-2

私は、プログラムでCollectionViewを作成し、それに画像を渡しました。今、各画像ビューをタップするには、次のそれぞれのView Controllerを開く必要があります。私はSwiftの初心者で、Swift 3でこの解決策を見つけることができませんでした。 4番目の 'todolistbutton'画像ビューでそれをやろうとしましたが、エラーが表示されました。誰でもSwift 3で私を助けてくれる? 私のコードは次のとおりです。コレクションビューセルを新しいコントローラにリンクする

import UIKit 

class MeBottomViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 



@IBOutlet weak var collectionView: UICollectionView! 


let imageArray = [UIImage(named: "guidebutton"), UIImage(named: "mybellybutton"), UIImage(named: "appointmentbutton"), UIImage(named: "todolistbutton"), UIImage(named: "duedatebutton"), UIImage(named: "myweightbutton")] 


override func viewDidLoad() { 
    super.viewDidLoad() 

    let tap = UITapGestureRecognizer(target: self, action: #selector(tappedme)) 
    imageArray[3].addGestureRecognizer(tap) // Error: Value of type 'UIImage?' has no member 'addGestureRecognizer' 

    imageArray[3].userInteractionEnabled = true // Error: Value of type 'UIImage?' has no member 'userInteractionEnabled' 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    return 6 
} 


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cells = collectionView.dequeueReusableCell(withReuseIdentifier: "cells", for: indexPath as IndexPath) as! SecondCollectionViewCell 

    cells.imageView?.image = self.imageArray[indexPath.row] 

    return cells 
} 

func tappedme() { 


    let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "ToDoTableViewController") as! ToDoTableViewController 

    self.navigationController?.pushViewController(secondViewController, animated: true) 


} 
} 
+0

あなたは 'UIImageView'に' UITapGestureRecognizer'を追加したいと思いますが、あなたはそれらを 'UIImage'に追加しようとしています... –

+0

私はそこにすべてのコードを与えているので、あなたは答えを凝ってくれますか?私はそれぞれのimageviewsをタップして別々にビューコントローラを開く必要があります。私は迅速に新しいです。 Plzzz ....ありがとうございます。 –

答えて

0

あなたはどんなジェスチャー

を設定する必要がいけません
import UIKit 

class MeBottomViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 



@IBOutlet weak var collectionView: UICollectionView! 


let imageArray = [UIImage(named: "guidebutton"), UIImage(named: "mybellybutton"), UIImage(named: "appointmentbutton"), UIImage(named: "todolistbutton"), UIImage(named: "duedatebutton"), UIImage(named: "myweightbutton")] 


override func viewDidLoad() { 
super.viewDidLoad() 




} 

override func didReceiveMemoryWarning() { 
super.didReceiveMemoryWarning() 
// Dispose of any resources that can be recreated. 
} 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

return 6 
} 


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

let cells = collectionView.dequeueReusableCell(withReuseIdentifier: "cells", for: indexPath as IndexPath) as! SecondCollectionViewCell 

cells.imageView?.image = self.imageArray[indexPath.row] 

return cells 
} 


} 



func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 

{ 

let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "ToDoTableViewController") as! ToDoTableViewController 

self.navigationController?.pushViewController(secondViewController, animated: true) 


} 
+0

彼が言ったように、彼はセルフタップで彼の新しいコントローラを起動したくないかもしれない、彼は彼の細胞に他のコンテンツを持っている場合、彼はタップを追加したい。この解決法も回避策です –

+0

かもしれませんが、彼はコレクションビューに画像を渡し、コードには他のコンテンツはありませんと言っています。なぜ私は彼にthis.otherを提案しています他の私は@ジミージェームズあなたの答えに同意します –

+0

もちろん、あなたの編集は私のものと同じように働いています:)今、私たちは彼のフィードバックを待っています –

0

あなたがUIImageUITapGestureRecognizerを追加することはできません。だから、 に必要な以下の通りごUIImageViewUITapGestureRecognizerを追加します。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cells = collectionView.dequeueReusableCell(withReuseIdentifier: "cells", for: indexPath as IndexPath) as! SecondCollectionViewCell 
    cells.imageView?.image = self.imageArray[indexPath.row] 

    // Tap gesture 
    let tap = UITapGestureRecognizer(target: self, action: #selector(tappedme)) 
    cells.imageView!.addGestureRecognizer(tap) 

    return cells 
} 

それからちょうど空viewDidLoadを聞かせて:

override func viewDidLoad() { 
    super.viewDidLoad() 
} 
+0

私はちょうどポストしようとしていた...それもシミュレータで動作しません。 @ Yimmy James –

+0

'cellForItemAt'メソッドでXcodeにブレークポイントを設定しようとしましたか? –

+0

はい...私はfunc collectionView(_ collectionView:UICollectionView、cellForItemAt indexPath:IndexPath) - > UICollectionViewCell {...でブレークポイントを保持していました。 @ Jimmy James –

0
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 

{ 

let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "ToDoTableViewController") 

?.pushViewController(secondViewController, animated: true) 
} 

@ matloob Hasnain。今それはついに働いた。私はそれを取り除かなければならなかった! ToDoTableViewController。

関連する問題