Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access my viewController from my appDelegate? iOS

I have an iOS app I created as a "view-based app" in xCode. I have only one viewController, but it is displayed automatically, and I see no code that ties it to my appDelegate. I need to pass data from my appDelegate to my viewController, but don't know how to pull that off.

My app delegate.h:

#import <UIKit/UIKit.h>   @interface AppDelegate : UIResponder <UIApplicationDelegate>  @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) NSDictionary *queryStrings;  @end 

Also, appDidFinishLoadingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     // Override point for customization after application launch.     [[JMC sharedInstance] configureJiraConnect:@"https://cmsmech.atlassian.net/"           projectKey:@"WTUPLOAD" apiKey:@"7fc060e1-a795-4135-89c6-a7e8e64c4b13"];      if ([launchOptions objectForKey:UIApplicationLaunchOptionsURLKey] != nil) {         NSURL *url = [launchOptions objectForKey: UIApplicationLaunchOptionsURLKey];         NSLog(@"url received: %@", url);         NSLog(@"query string: %@", [url query]);         NSLog(@"host: %@", [url host]);         NSLog(@"url path: %@", [url path]);         queryStrings = [self parseQueryString:[url query]];         NSLog(@"query dictionary: %@", queryStrings);     }     else {         queryStrings = [self parseQueryString:@"wtID=nil"];     }      return YES; } 
like image 357
HackyStack Avatar asked Apr 04 '12 16:04

HackyStack


People also ask

How do I get ViewController view?

If you need to find the view controller that is responsible for a particular view, the easiest thing to do is walk the responder chain. This chain is built into all iOS apps, and lets you walk from one view up to its parent view, its grandparent view, and so on, until it reaches a view controller.

What is iOS ViewController?

A view controller acts as an intermediary between the views it manages and the data of your app. The methods and properties of the UIViewController class let you manage the visual presentation of your app. When you subclass UIViewController , you add any variables you need to manage your data in your subclass.

What is AppDelegate file in iOS?

In all the apps built prior iOS 13, AppDelegate is the main entry of the app and it is the place where many logics and app states will be handled. It is the place where application launch and apps foreground and background logics are handled.


1 Answers

You can access it with:

MyViewController* mainController = (MyViewController*)  self.window.rootViewController; 

If you are nesting your view behind a tabviewcontroller or navigation controller it will return that to you and you will need to access your view controller inside of it

like image 172
utahwithak Avatar answered Oct 13 '22 22:10

utahwithak