1

私は、viewViewrollerをcollectionViewからプログラムで(ストーリーボードなしで)表示するのに問題があります。私はこれがかなり簡単だと思うだろうが、私はいくつかの困難を抱えているが、これは私がいくらか新しいことを迅速にしているからだ。私はcollectionViewCellsがデフォルトでデリゲートではないと思うので、私はcollectionViewクラスでdidSelectItemAtを実装しようとしましたが、まだ運がありません。セルのタップで、私は印刷の声明を受けています、ちょうどショーセグを持っていません。下記のcollectionViewCellコードをご覧ください。viewViewController from storyboardsなし

// collectionViewCell class 
class PlanningCell: BaseCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

    lazy var collectionView: UICollectionView = { 
     let layout = UICollectionViewFlowLayout() 
     let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     cv.dataSource = self 
     cv.delegate = self 
     return cv 
    }() 

    //collectionViewCell variables 
    var plannedPlaces = [CurrentPlanner]() 
    let cellId = "cellId" 

    var basePlanningCell: BasePlanningCell! 

    override func setupViews() { 
     super.setupViews() 
     addSubview(collectionView) 

     collectionView.register(BasePlanningCell.self, forCellWithReuseIdentifier: cellId) 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return plannedPlaces.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! BasePlanningCell 
     cell.currentPlanner = plannedPlaces[indexPath.item] 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     print("did tap") 
     let plannersDetailVC = PlanningPlaceDetailsVC() 
     plannersDetailVC.navigationTitle = plannedPlaces[(indexPath.row)].planningPlace 
     // below I am receiving the use of unresolved identifier "show" error 
     show(plannersDetailVC, sender: self) 
    } 

} 

答えて

0

通常、あなたは別のビューcontroolerを提示したいですこの関数を呼び出す必要があります

self.present(yourViewController, animated: true, completion: nil) 

しかし、presentはビューコントローラーでのみ使用できるため、印刷を行う場所ではできません。だから今問題は、どのように親ビューコントローラにあなたのセルのクリックを指示し、文字列を渡すことになります。 2つのオプションがあります。thisのようなデリゲートを作成することも、NSNotificationCenter post callを作成することもできます。もう1つは簡単です。

あなたが親ビューコントローラにセルをクリックしてイベントを渡すと、あなたは私がすべてのストーリーボードを使用していない別のビューのcontrooler

+0

ありがとうございましたが、今、私にエラーメッセージ "PlanningCellの値はタイプメンバ 'present'がありません" – user3708224

+0

@ user3708224更新された私の答え –

+0

ありがとう、ありがとう。私はそれらのオプションを調べます。 – user3708224

0
let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "VCIdentifier") as! PlanningPlaceDetailsVC 
     present(vc, animated: true, completion: nil) 

PlanningPlaceDetailsVC 識別子にコードによってsetted VC用に更新

設定することを忘れないでください:

let vc : UIViewController = PlanningPlaceDetailsVC() 
self.presentViewController(vc, animated: true, completion: nil) 
+0

に移動するために、上記のそのメソッドの言及を呼び出して起動することができます。 – user3708224

+0

回答が更新されました。 –

関連する問題