我想在按下uibutton时按下UICollectionViewController。我测试了一些,当我按下按钮时,它导航到一个空白的ViewController。
Root View Controller(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 :(得分:1)
如果您正在使用故事板,请使用此代码...
nil,foo,*nil,bar = ['no','foo','nop','not at all','bar']
答案 1 :(得分:-1)
使用此代码 -
让mealController = self.storyboard?.instantiateViewControllerWithIdentifier(“你的控制器标识符名称在这里”)作为MealCollectionViewController self.navigationController?.pushViewController(mealController,animated:true)