0

I was trying to create a simple app which implement UICollectionViewController programmatically. I have created some cells and when user clicks on particular cell should move to another controller. But i am facing problem which view is not pushing to next view controller.

Here is i setup rootViewController in appDelegate

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    let flow = UICollectionViewFlowLayout()
    let collectionView = ViewController(collectionViewLayout: flow)
    let navigation = UINavigationController(rootViewController: collectionView)
    window?.rootViewController = navigation

Here is how i created UICollectionView

private let cellId = "cellid"
var viewController: ViewController?
override func viewDidLoad() {
    super.viewDidLoad()
    collectionView?.backgroundColor = .white
    collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
    // Do any additional setup after loading the view, typically from a nib.
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
    cell.backgroundColor = .red
    return cell
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: 210)
}

Here is how i control when user click on particular

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print(indexPath.item)
    viewController?.pushView()
}

func pushView() {
    let controller = UINavigationController(rootViewController: DetailController())
    self.navigationController?.pushViewController(controller, animated: true)
}

even the function is called but it never push to new view controller

I Love Coding
  • 580
  • 5
  • 13
  • I already set `UICollectionViewController` as UINavigationController – I Love Coding Jul 20 '18 at 04:33
  • window = UIWindow(frame: UIScreen.main.bounds) let flow = UICollectionViewFlowLayout() let collectionView = ViewController(collectionViewLayout: flow) let navigation = UINavigationController(rootViewController: collectionView) window?.rootViewController = navigation window?.makeKeyAndVisible() – I Love Coding Jul 20 '18 at 04:33

3 Answers3

2

First you have to make sure your current view controller is having navigation view controller, you can just check by printing the object in viewDidLoad method print(self.navigationViewController). If printing nil then you have to make the navigation controller followed by these steps - SelectYourVicotroller -> Go to Editor menu option -> Embed to -> Navigation Controller

func pushView() {

    let controller =  DetailController()
    //OR

//Add the storyboard identifier of your view controller - "DetailController"

    let controller =  self.storyboard.instantiateViewController(withIdentifier: "DetailController") as! DetailController

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

}

Call this method into didSelect method

Ashish
  • 116
  • 5
  • He is not using storyboard. He set the rootViewController in appdelegate – Visal Sambo Jul 20 '18 at 07:15
  • Paste this lines into your AppDelegate's applicationDidFinish method . self.window = UIWindow(frame: UIScreen.main.bounds) self.window?.makeKeyAndVisible() window?.rootViewController = UINavigationController(rootViewController: YourCollectionViewCotroller) – Ashish Jul 20 '18 at 07:36
  • @Ashish already added. and i also edited my question as well. The current view keeps not pushing to another view, but the i did breakpoint of the statement (statement of function pushView()) is called but it is not pushing.... – I Love Coding Jul 20 '18 at 08:47
  • @VisalSambo in your code seems you have not initialized the viewcontroller object you just declared. Call the pushView() method followed by self or just paste this in didSelect "pushView()" – Ashish Jul 20 '18 at 09:29
  • @Ashish you meant in didSelect I should type ‘self.viewController.pushView()’ ? – Visal Sambo Jul 20 '18 at 09:32
  • self.pushView() – Ashish Jul 20 '18 at 09:32
1

Please try this:

let objMainController : yourViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourViewController") as! yourViewController

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = objMainController

Please embed navigation controller from storyboard. It may helps you.Thank you.

Sanjukta
  • 1,057
  • 6
  • 16
0

Change

viewController?.pushView()

To

self.pushView()
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Get rid of pointless `viewController` property. You are the view controller, `self`. – matt Jul 20 '18 at 10:49