jason white
jason white

Reputation: 711

iOS Xcode 4.2 using StoryBoard to present a ViewController

The new way of presenting a viewcontroller using the StoryBoard.

    UIStoryboard* secondStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

UINavigationController* secondViewController = [secondStoryboard instantiateViewControllerWithIdentifier:@"Connect"];

[self presentViewController: secondViewController animated:YES completion: NULL];

The old way of presenting the controller Connect is like this

  Connect *connect = [[[Connect alloc] initWithNibName:@"Connect" bundle:nil] autorelease]; 

[self presentViewController:connect animated:YES completion:NULL];
 NSString *userid;
 userid=@"123";

 [connect setID:userid];

I want to call the setID function of the connect controller in the Storyboard way, how can I do that? Seems like I don't get an instance of Connect controller directly.

Upvotes: 2

Views: 1020

Answers (2)

Aaron Brager
Aaron Brager

Reputation: 66252

You should subclass the view controllers so that you can control what happens when users interact with it (unless your app can function on segues alone.)

In Xcode, File -> New -> File -> Cocoa Touch Class. Make a class like MyAwesomeViewController that subclasses (in your case) UINavigationController.

I like to make a custom method called NewVC in my custom view controller classes. It can do everything you list above, plus any custom setup:

+(MyAwesomeViewController *)NewVC {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @"MyStoryboard" bundle: nil];
    return [storyboard instantiateViewControllerWithIdentifier: @"MyAwesomeViewController"];
}

This way when you want to create a new one you can just call [MyAwesomeViewController NewVC] and it'll return a new view controller instance.

Upvotes: 1

spring
spring

Reputation: 18497

Why are you casting it as UINavigationController? Just do what you were doing.

Connect* connect = [secondStoryboard instantiateViewControllerWithIdentifier:@"Connect"];

Upvotes: 0

Related Questions