0

I am dynamically creating 9 cells that will fit into a UICollectionView. When tapped, each button will load a different view controller. As the cells are generated by data from an array, so I do not have the ability to set a segue with an identifier in Interface Builder.

I have been using this method: self.navigationController?.pushViewController(vc, animated: true), but I believe that it is not the most up-to-date way to transition to a different view controller.

The Questions:

  • Should I create a segue for each dynamic cell programically, if so: how?
  • Should I connect the one button in interface builder to a dummy view controller and switch out the destination view controller before the segue begins, if so: how would I switch out the destination view controller?

Interface Builder screenshot of setup:

Interface Builder Setup

Jake Chasan
  • 6,290
  • 9
  • 44
  • 90

2 Answers2

2

You should set up segues in the Interface Builder but not from a button but from a view controller and set appropriate identifiers. Use this method to start a segue:

- performSegueWithIdentifier:sender:
Yaroslav
  • 2,435
  • 1
  • 19
  • 36
  • but if the buttons are dynamically generated (they do not appear in Interface Builder), how am I supposed to set up the segue in Interface Builder? – Jake Chasan May 29 '15 at 21:35
  • You can set up UIButton.Tag with a segue identifier. [self performSegueWithIdentifier:(sender as UIButton).Tag sender:self]; – Yaroslav May 29 '15 at 21:39
  • @JakeChasan: Segues cannot be setup outside of IB but can be triggered using `performSegueWithIdentifier:sender:` once setup in IB. If you cannot setup the segue in IB (not sure why you couldn't as a segue doesn't have to be set on a `UIButton`) then see this answer: http://stackoverflow.com/a/9675186/558933 – Robotic Cat May 29 '15 at 21:40
  • @RoboticCat: the hard part for me to follow is what two objects do I link up in interface builder if all of the cells are dynamically added and each goes to a different view controller, and there is only one cell present in the interface builder? – Jake Chasan May 29 '15 at 21:43
  • 1
    Hold Ctrl and Drag&Drop from a ViewController rather than from a button. – Yaroslav May 29 '15 at 21:43
  • @Yaroslav: and drag the mouse to what object? the dummy cell? – Jake Chasan May 29 '15 at 21:45
  • 1
    To the ViewController you want to have a segue to. http://i.imgur.com/eR65DfZ.png – Yaroslav May 29 '15 at 21:46
  • @Yaroslav: but if each button in the collection view segues to a different view controller, and they are dynamically created, which view controllers would I want to connect, since I can only connect one view controller this way? – Jake Chasan May 29 '15 at 21:52
  • 1
    You connect your ViewController to every one of them. Give each segue a different identifier. Then specify which identifier to use in performSegueWithIdentifier:sender: in (for example) a UIButton's Tag property. – Yaroslav May 29 '15 at 21:54
  • @Yaroslav: Got it. Thank you very much for your help. – Jake Chasan May 29 '15 at 22:00
1

I wouldn't use segues for this scenario. I would give each of the different view controllers a storyboard ID and then load them that way. Then you can load the specific view controller by that ID and transition that way.

I am making some assumptions about your code so I can show an example. I am going to assume that each cell has a button in it and that is what is being pressed to trigger a segue. I will also assume that each of these cells' buttons have the initial view controller as the target, with a method declaration like the one in the code below.

func buttonPressed(cell: UICollectionViewCell) {
    let viewController = self.storyboard!.instantiateViewControllerWithIdentifier("storyboardID") as! UIViewController
    self.navigationController!.showViewController(viewController, sender: self)
}
keithbhunter
  • 12,258
  • 4
  • 33
  • 58
  • In this approach, how would one control the type of transition that is performed when this method is called? – Jake Chasan May 29 '15 at 21:47
  • I mean, lets say I wanted to show a "flip" segue instead of the standard "push" segue with the showViewController method. How would I specify the segue that is performed? (since the cells are dynamically created and thus there are no configurable options in Interface Builder? – Jake Chasan May 29 '15 at 21:50
  • You would use the different methods listed in the [UIViewController Presenting Content docs](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/) – keithbhunter May 29 '15 at 21:53
  • So lets say I wanted to call the following method to configure the segue: performSegueWithIdentifier, what should I use as the "identifier"? – Jake Chasan May 29 '15 at 21:56
  • You wouldn't use `performSegueWithIdentifier` if you instantiate the view controller in code. Stick to the methods in the docs I linked in the last comment. Specifically, the methods in the `Presenting Content Over the View Controller` section. – keithbhunter May 29 '15 at 22:01
  • Thanks, I will look into the methods in the link. – Jake Chasan May 29 '15 at 22:03