1

I have a view controller reference to a storyboard of a given identifier. I'm adding a bunch of buttons to it, then trying to display it via a segue.

My problem is that when the segue fires, it creates a difference instance of the view controller with the same segue identifier, and thus it's blank.

What's the best practice to addSubView() to a storyboard, then getting that SAME storyboard object to display?

CLARIFICATION

Here's the flow I'm using:

Central VC -> Create SubVC using centralized Storyboard Object -> Adding SubViews to that SubVC in a factory class -> Queue Segue from SubVC back to the Central VC for segue using its identifier -> [it creates a NEW VC without my additions]

Mark Löwe
  • 572
  • 1
  • 10
  • 24
  • It’s not clear what you’re trying to do – it would be helpful if you could add some code showing how you’re creating the view and adding the buttons. – Stuart Mar 06 '16 at 10:37

2 Answers2

1

You can instantiate a view by code, and pushing in to you navigation controller, it's a clean approach and dont mess the Storyboard with unnecessary segues.

Just instantiate the next view (you must first give this view an StoryBoard ID), call it by code, and push it in the navigation controller.

Objective-C

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"storyBoard_Name" bundle:nil];
UIViewController* controller = [storyboard instantiateViewControllerWithIdentifier:@"ViewController_ID"];

[self.navigationController pushViewController:viewControllerName animated:YES];

Swift

let storyboard = UIStoryboard(name: "storyBoard_Name", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ViewController_ID") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
Ulysses
  • 2,281
  • 1
  • 16
  • 24
  • I tried the Swift version of this and it didn't work. :( – Mark Löwe Mar 06 '16 at 22:16
  • It should, I will add a swift version, hold a sec. – Ulysses Mar 06 '16 at 22:18
  • @MarkLöwe, check if it works, or post the error message. – Ulysses Mar 06 '16 at 22:30
  • I think I got the cart before the horse. I thought a storyboard locked the instances into a singleton so that adding to them by name would merely prep them before display. I figured out above that I have to call the segue first, then send the prep calls to the next VC to ensure that it exists prior. Arg. – Mark Löwe Mar 07 '16 at 00:27
1

If you're using segues, then the Storyboard creates the destination viewController. If you want to customize the destination viewController, then you do that in prepareForSegue.

vacawama
  • 150,663
  • 30
  • 266
  • 294
  • I'm creating a viewController using a centralized StoryBoard object, then adding everything to that. Then I queue the segue from the same class that instantiated the VC, and it creates another one on the fly. :( I need the one I created using the identifier to load the same one. – Mark Löwe Mar 06 '16 at 22:11
  • If you want to use segues, then you need to trigger the segue first, then in `prepareForSegue` get the destination viewController from `segue.destinationViewController as! YourViewController` and then add your extra views to that. – vacawama Mar 06 '16 at 22:20