2016-10-11 4 views
0

私はuibuttonを押したときにUICollectionViewControllerをプッシュしたいと思います。私はいくつかのテストをして、私はそれを空のViewControllerにナビゲートするボタンを押します。pushViewController UICollectionviewControllerにuibuttonを使用するプログラムでSwift

ルートビューコントローラ(CategoryViewController)

class CategoryViewController: UIViewController { 



override func viewDidLoad() { 
    super.viewDidLoad() 

    //UIButton 

    let image = UIImage(named: "menudo") as UIImage? 
    let button = UIButton(type: UIButtonType.Custom) as UIButton 
    button.frame = CGRectMake(100, 100, 100, 100) 
    button.setImage(image, forState: .Normal) 
    button.addTarget(self, action: #selector(CategoryViewController.action(_:)), forControlEvents:.TouchUpInside) 
    self.view.addSubview(button) 

} 

func action(sender: UIButton!) { 
    let layout = UICollectionViewLayout() 
    let collectionViewController = MealCollectionViewController(collectionViewLayout: layout) 
    navigationController?.pushViewController(collectionViewController, animated: true) 

} 

私は私がuibutton(MealCollectionViewController)を押したときに、こののViewControllerを表示したい

import Foundation 
import UIKit 


class MealCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    //Creating the CellId for reusable cell 

    private let cellId = "cellId" 


    override func viewDidLoad() { 
      super.viewDidLoad() 

     //Creating CollectionView 

     collectionView?.backgroundColor = UIColor.whiteColor() 
     //setting the cellId reusable to show on screen USE CATEGORY CELL BECAUSE MEALCOLLECTION VIEW IS NOT A CELL 

     collectionView?.registerClass(CategoryCell.self, forCellWithReuseIdentifier: cellId) 
    } 


    //Creating the layout for the cell 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) as! CategoryCell 
     return cell 
    } 


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 1 
    } 


    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
     return CGSizeMake(view.frame.width, view.frame.height) 
    } 
} 
+0

strotyboradを使用してRU、またはXIB –

+0

私が使用していますストーリーボード –

+0

次に私のコードを試してください。あなたのボタンのクリック方法。 –

答えて

1

あなたはストーリーボードを使用している場合、このコードを使用して...

let meal: MealCollectionViewController = (self.storyboard?.instantiateViewControllerWithIdentifier("STORYBIARD_CONTROLLER_IDEBTIFIER_NAME"))! as! MealCollectionViewController 
self.navigationController?.pushViewController(meal, animated: true) 
-1

このコードを使用する -

MealCollectionViewController self.navigationControllerとしてmealController = self.storyboard .instantiateViewControllerWithIdentifier( "ここではあなたのコントローラの識別名")しましょう.pushViewController(mealController、アニメーション:真)??

関連する問題