-1

I'm new to Storyboarding in objective c and I need to call method from UIVIewController. Before Storyboarding I was initializing UIViewController in AppDelegate or just assigning pointer there, and then simply call method from any class, accessing AppDelegate properties. How to do It in Storyboarding, If there are no UIViewController initializing by myself? My app is UITabBar application if it does matter.

Timur Mustafaev
  • 4,869
  • 9
  • 63
  • 109

2 Answers2

1

You can get the root view controller of your storyboard from your app delegate. For example:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UITabbarController *tbc = (UITabbarController *)self.window.rootViewController;

    // Do any setup you want to here
    // You aren't setting up the controller - just accessing the one that has been set up for you by the storyboard

    return YES;
}
Abizern
  • 146,289
  • 39
  • 203
  • 257
-1

If you want to have this method available from any class you can keep doing the same. Place in in your AppDelegate and then just retrieve the shared instance on the controller you want to use it from. (all your application has access to your appdelegate) so just import the header of the app delegate, create a new instance of your specific appdelegatename class, and call the shared delegate. then you can use the method as if that class would have it.

Additionaly you can use something like this

#import "AppDelegate.h"
#define appDelegate (AppDelegate *) [[UIApplication sharedApplication] delegate]

IF you want to call a method from ANY of your viewcontrollers which are specified in the storyboard you can do it by referencing their identifier:

PreviewViewController *tmp = [[self storyboard] instantiateViewControllerWithIdentifier:@"PreviewViewController"];
Pochi
  • 13,391
  • 3
  • 64
  • 104
  • Using the application delegate as a convenient place to store variables throughout your application breaks encapsulation. Also - that wasn't what the question was about. – Abizern Aug 13 '12 at 09:45
  • I know how to use AppDelegate shared instance. I need to know how to get initialized instnance of UIViewController in StoryBoard – Timur Mustafaev Aug 13 '12 at 09:48
  • @Abizern it is true that it breaks encapsulation but it really depends on the kind of method he wants to use throughout all his application, for instance core data methods and facebook api methods are often specified there. I have no way of knowing what kind of method or purpose he will use. – Pochi Aug 13 '12 at 09:53
  • @TimurMustafaev I only offered you an alternative in case you want to keep doing what you have been doing. It is not a reason to downvote in my opinion – Pochi Aug 13 '12 at 09:57
  • @LuisOscar Core data methods are created there as a convenience. you don't have to call back to the AppDelegate to use them, you can stand up your own stack in other view controllers - or even pass the managed object context up through the stack from the app delegate. – Abizern Aug 13 '12 at 10:27