4

I have four tabs in a UITabBarViewController. Each view has UIButtons added to their navbar's, and each tab has a "post button" that will take the user to the post page.

How do I make reference to a ViewController that is sitting in my storyboard already so that I can pass it into the PresentViewController: method?

for example, this is the method used when the post button is clicked from any of the four tab bar views:

- (void)postInvoked:(UIBarButtonItem *)sender
{
    [self presentViewController:______ animated:YES completion:nil];
}

What goes in the blank? I think I need to avoid referencing the UIViewController like this:

UIViewController * postPage = [[UIViewController alloc] init];

as this would create a new instance of the page every time the user navigates to the page.

Chisx
  • 1,976
  • 4
  • 25
  • 53
  • Do you need to go to the same instance each time (is there data in that controller that you want to persist between presenting it from the different tabs)? – rdelmar Mar 31 '14 at 01:03
  • Set storyBoardId for your viewController, and use then present or push using ' instantiateViewControllerWithIdentifier '. – Vineesh TP Mar 31 '14 at 04:41

3 Answers3

3

You have to create instance of controller that you want to present:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
UIViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier: @"<Controller ID>"];

This will create controller with preconfigured views from storyboard.

@"Controller ID" == "Storyboard ID" attribute in the Interface Builder. (Inspector tab when controller is selected)

And present it as you want.

[self presentViewController:controller  animated:YES completion:nil];
Bohdan Orlov
  • 274
  • 3
  • 3
  • Where should I place this instantiation if I want to simply reference the UIViewController from multiple classes. Should it be in AppDelegate.. I want to call a `PresentViewController:` from multiple classes, so I don't want to have to recreate the reference to the VC. – Chisx Mar 30 '14 at 22:02
  • I guess what I'm asking is.. is it necessary to use the top two lines you provided in every `UIViewController` that will need to traverse to the instantiated VC, or should that be declared somewhere like AppDelegate? – Chisx Mar 30 '14 at 22:21
  • That depends on your app logic. Default behaviour is that you creating in every UIViewController that you have. I don't think that your intent is to create it once and just present it, but if so you can create it once in `AppDelegate`. – Bohdan Orlov Mar 31 '14 at 11:47
  • If you want to keep only one instance of it, you should use [singleton pattern](http://stackoverflow.com/q/5720029/3455244). Thus will make your VC available throughout the app without need to keep it in app delegate. – Bohdan Orlov Mar 31 '14 at 11:52
  • Thanks man. I'm just creating an instance of it from each call in each class. I appreciate the singleton link though.. I didn't know about that. – Chisx Apr 01 '14 at 03:45
1

It depends.

A VC that is "sitting in your storyboard" doesn't exist. That's just a template that lets you create new copies of the VC when you need it.

If you already have a copy of this VC in memory, just pass a pointer t out in your call to presentViewController:animated.

If not, you need to give it a unique ID in the storyboard, and then use the UIStoryboard method instantiateViewControllerWithIdentifier: to create an instance of the VC, then pass a pointer to the newly-created VC to presentViewController:animated

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • But where should the reference of the UIViewController go? `AppDelegate`? or inside each VC of the tabBarVC. I want to make sure that when I call `presentViewController:` from each of the tabVC's, it uses the same reference. – Chisx Mar 30 '14 at 22:15
1

1)In the story board, click on the viewcontroller you want to present then in the utilities pane, click on the identity inspector.

2)In the storyboard ID enter any string, for example "postView".

3)Edit the method to this:

- (void)postInvoked:(UIBarButtonItem *)sender
{
    [self presentViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"postView"] animated:YES completion:nil];
}

In other words:

Assuming that you add the storyboard ID, the blank space should be filled in with

[self.storyboard instantiateViewControllerWithIdentifier:@"storyBoardIDHere"]

Hope this helps.

DominicanCoder
  • 365
  • 2
  • 13