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
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
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With