Call AppDelegate Method from Class - iphone

Basically, I need to call a method in my AppDelegate from one of my view controller classes.
Currently, I'm doing the following:
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate doMethod];
And including the myAppDelegate.h at the top of the .m file of the class:
#import "myAppDelegate.h"
When I run it, everything works...
But I get the following warning:
warning 'myAppDelegate' may not respond to '-doMethod'
Is there another way that I should reference the app delegate?
Thanks for any help in advance.
EDIT: FIXED:
All I had to do was declare the method in the .h file of the AppDelegate:
-(void)doMethod;

adding to Marks comment. Is the -(void) doMethod; declared in the appDelegate header file and is the appDelegate.h file imported in the file you try to call the method from:)
Sorry should have put it in as an answer in the first place, so the question does not look unanswered :/

Related

Using #define to access App Delegate Object doesn't work

I am using App Delegate object in different classes. I want access it in whole project. Am define this object in Prefix.pch file as
#define Appdelegate (AppDelegate *)[[UIApplication sharedApplication] delegate]
but problem is that Appdelegate variable does not access App delegate variable.
it Shows error.
but if am use this code works fine
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.variablename;
Am I doing it correct or is there a way of doing what I do?
thanks in Advance.
Should be:
#define Appdelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
// ^----------------------parenthesis--------------------------^
As this question and its answers demonstrate, leaning on the preprocessor is usually a bad idea.
It's easy to get things wrong. We all develop good instincts for operator precedence, but the preprocessor has no clue. To defend against unexpected context problems, you often need to wrap everything in extra parens and braces.
It's easy to convince yourself that you're doing one thing when the code is doing another. When things break, neither the compiler's error messages nor the debugger are likely to help much.
Most important, the preprocessor can let you take a bad idea and spread it pervasively through the program. The bad idea here is using the App Delegate globally.
Global variables are a code smell. Efforts to make global variables even more global by stuffing them into precompiled headers are a code smell. If the framework thought you should have access to the AppDelegate from everywhere, you wouldn't need to jump through these (modest) hoops!
So, when tempted to do something like this, it's nice to know the preprocessor is there and the pch headers are there, but keep in mind that you're fighting the framework and almost certainly making a design error. That might be OK in your context, but it's something to know.
You need to import AppDelegate file where you are declare delegate macro:
#import "AppDelegate.h"
And then define
#define Appdelegate (((AppDelegate *)[[UIApplication sharedApplication] delegate]))

Using 1 AppDelegate for multiple .xib xcode/ipad sdk

Is it a good practice to use one main AppDelegate.h to handle all the ibaction stuff?
is it even possible? if so who does one do this? my IB only lets me link to the associated .m file
firstView.xib only respond to ibaction in firstView.m
I want a button on firstView.xib to respond to ibaction in AppDelegate.m
any thoughts?
No, it's not good practice. But if you really want to...
If you aren't worried about having the same instance of appDelegate holding the IBActions, you can just drag a generic object from the library into your firstView.xib, then change the class to appDelegate. That will allow you to link actions.
You can hook up the actions programmatically: create IBOutlets from the view controller, and in the viewDidLoad method of the view controller, get the [[UIApplication sharedApplication] delegate] and attach it's IBAction methods to the actions of the IBOutlets for the buttons
To access the app delegate from elsewhere in your code, do the following:
#import "AppDelegate.h"
AppDelegate * appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

How to access the tabBarController from FirstViewController?

Created a new project from the Tab Bar Application template, how can I access tabBarController (which is declared in AppDelegate) from FirstViewController? I'd like to read some attributes in the tabBarController from the view controller. Thanks!
You can always access the app delegate as follows:
[UIApplication sharedApplication].delegate
So you should be able to get at the tabBarController by doing this:
MyAppDelegate *delegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;
Now refer to delegate.tabBarController.
[In the above, MyAppDelegate is whatever name you gave the class of your app delegate.]

Refer to an integer from one ViewController from another

I'm unable to refer to my integer pickedItem declared in my RootViewController.h in my DetailViewController.m file.
Not sure if I should declare this as a global variable, but tried unsuccessfully to do so.
I think this should be simple, but I haven't got any other suggestions (from other posted answers) to work.
Thanks in advance.
You can access the app delegate through [[UIApplication sharedApplication] delegate] this call. And then can access the root view controller's property (assuming you have access to the rootViewController object in appDelagate).
In RootViewController.h
#property(nonatomic, assign) NSInteger myInt
And from anywhere in the code
UIApplicationDelegate *delegate = [[UIApplication sharedApplication] delegate];
delegate.viewController.myInt = 31;
p.s. I have just typed the code, not compiled. So there might be some typo.
You just wouldn't want it coded this way. I recommend taking a look at NSNotificationCenter, or create a delegate class for yourself, to communicate to the DetailViewController of page changes. There shouldn't be a dependency from detail -> root the way you have it.
Based on your code above, I don't see where you're setting rootViewController on detailViewController? Is it nil? I'd think referencing rootViewController.pickedItem if rootViewController was nil would cause a crash, but worth checking.
Also, is pickedItem being set appropriately? In other words, is it set before the detail code is being called?

Passing objects from AppDelegate.m to View Controller

Hi I have an iphone application in which I am fetching & parsing data in in applicationDidFinishLaunching. Now I want to transfer this fetched data which is in one NSMutableArray to my first view controller to display it there.
Whats the best way of doing this...
You can past it through the init like initWithDataArray: , I think that is good enough.
Another solution, which imo is worse, is parsing the AppDelegate to the ViewController and then you can call : appDelegate.dataArray but it will leak out too much information
Application Delegate is what i prefer in this case.
//Declare NSMutableArray object in AppDelegate.h ,Now property & synthesize it
//Store your data in NSMutableArray object
//Create ApplicationDelegate object any your View Controller Class
yourApplicationAppDelegate *appDelegate;
appDelegate = (yourApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
Now you can access NSMutableArray object through appDelegate any where in your application