Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing AppDelegate Methods - iphone

Tags:

xcode

iphone

I have an AppDelegate which has 3 views. I add all three

[window addSubview:gameViewController.view];
[window addSubview:viewSettings.view];
[window addSubview:viewController.view];
[window makeKeyAndVisible];

In the app delegate, i have some methodes for swapping views by calling

[window bringSubviewToFront:gameViewController.view]; 

When i am inside viewController, I use

pinkAppDelegate *appDelegate=  (pinkAppDelegate *)[[UIApplication sharedApplication] delegate];

[appDelegate switchToSettings];

to switch my subviews...so far so good.

BUT, when I'm in my viewSetting UIViewController, and do the same appDelegate call, it chocks, like it doesn't understand how to call the appDelegate method.

I've got all my views hooked in my mainwindow xib, but can't figure out why i can't traverse the methods in the main appdelegate

like image 563
jalo Avatar asked May 12 '09 03:05

jalo


2 Answers

For reference there is still a typo in the above, also I have another addition which helps avoid warnings of having to cast every time. if you use:

#import "MyAppDelegate.h"
#define myAppDelegate (MyAppDelegate *) [[UIApplication sharedApplication] delegate]

I put the above in a constants.h and then can use

[myAppDelegate doSomething];

anywhere I import constants.h

like image 44
agough Avatar answered Oct 07 '22 13:10

agough


Inside viewController, you're setting up a local variable called appDelegate that points at your app delegate. That's what this line does:

pinkAppDelegate *appDelegate= (pinkAppDelegate *)[[UIApplication sharedApplication] delegate];

This variable is local to viewController, so you can't use it in the settings view controller. You need to set up another variable there.

Alternatively, use this nice #define throughout your app. (You'll need to put it in a .h header file that you include in every file in your project.)

#define myAppDelegate (MyAppDelegate *)[[UIApplication sharedApplication] delegate]

Then you can do the following anywhere:

[myAppDelegate doSomething];
like image 164
Jane Sales Avatar answered Oct 07 '22 14:10

Jane Sales