3

I'm using the following code to goto another view programmatically in swift 3. There is no error while running. But don't know why it is not going to that view

Code I used:

let images = self.storyboard?.instantiateViewController(withIdentifier:"Collection") as! UICollectionViewController
            self.navigationController?.pushViewController(images, animated: true)

I want to goto CollectionView.swift enter image description here

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Gijo Varghese
  • 11,264
  • 22
  • 73
  • 122

5 Answers5

6

In order to navigate to between view controllers you use UINavigationController.

I will provide you a basic example of navigation, hopefully it will help you to make navigation work in your project.


Result

ViewController passes an image to DetailViewController between navigation:


Setting up your Views

First ensure that your root controller is embedded with a navigation controller control so that you can navigate using segues:

enter image description here

Connect your views that are being used to navigate.

enter image description here


Code

class ViewController: UIViewController {

  @IBOutlet weak var imageView: UIImageView!
  
  override func viewDidLoad() {
    super.viewDidLoad()
  }
  
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    // showDetail Segue
    if segue.identifier == "showDetail" {
      // Sending the image to DetailViewController
      // Before appears in the screen.
      let detailViewController = segue.destination as! DetailViewController
      detailViewController.image = sender as? UIImage
    }
    
  }

  @IBAction func loginButton(_ sender: AnyObject) {
    
    // Go to another view controller
    performSegue(withIdentifier: "showDetail", sender: imageView.image)
    
  }

}

class DetailViewController: UIViewController {
  @IBOutlet weak var imageView: UIImageView!
  var image: UIImage?
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    if let imageSent = image {
      imageView.image = imageSent
    }
  }
}
Community
  • 1
  • 1
Wilson
  • 9,006
  • 3
  • 42
  • 46
0

I think it would be best instead of programmatically calling the Storyboard ID of the controller, to use Segues instead. You can find out how to use them here.

But if you have to use the SBID, here is an example snippet from one of my projects...

let vc = self.storyboard?.instantiateViewController(withIdentifier: "UpdateFilesViewController")
self.navigationController?.present(vc!, animated: true, completion: nil)

Notice that presenting might be the solution you are after instead of pushing. Additionally, you seem to be casting the UIViewController to a UICollectionViewController, which may cause problems as well. Keep note that a UICollectionViewController is a subclass of a UIViewController so the casting is unnecessary.

dovedevic
  • 673
  • 2
  • 14
  • 33
  • Based on your recent update, it seems you are searching for an identifier with name "CollectionOfImages" yet in your stroyboard, you say it is named "Collection". Make sure these match up. – dovedevic Sep 29 '16 at 03:49
  • No, i just renamed it for testing – Gijo Varghese Sep 29 '16 at 03:55
  • What is the segue in the image you are using which is provided in your first image? Can you describe you storyboard elements in an edit. I will try to replicate on a test project. – dovedevic Sep 29 '16 at 04:08
0

As we found out in the comments, your current viewController does not have a navigationController.

This means that

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

does nothing. You need to set up the initial viewController with a navigationController for this to have any effect.

T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32
0

Change your storyboard Identifier

You just need to change your storyboard identifier as "CollectionOfImages" in identity inspector as Storybroad identity so it will move on there.

Note : Please make sure that your current view controller is embed with navigation controller otherwise it will not push new controller during movement.

Pavan Gandhi
  • 1,729
  • 1
  • 22
  • 36
0

If you don't want to use navigation controller, you can simply present your controller using:

let images = self.storyboard?.instantiateViewController(withIdentifier:"Collection") as! UICollectionViewController

self.present(images, animated: true, completion: nil)
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • Getting error: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] instantiated view controller with identifier "Collection" from storyboard "Main", but didn't get a UICollectionView.'" – Gijo Varghese Sep 29 '16 at 05:41