Reputation: 411
I have a global variable defined in Appdelegate.I want to use in other controller. And I can use like this :
AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
appdelegate.name=[NSString stringwithFormat:@"%@",ename];
But whereever I want to access appdelegates variable in viewController I have to use AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
each time which gives warning messages like 'Local declaration of AppDelgate hides instance variables '.So is there a way that I could declare it only once any access it many times in a ViewController.How can I get rid of this warning?
EDIT:
.h :
#import "AppDelegate.h"
@interface More : UIViewController
{
AppDelegate *appdelegate;
}
.m:
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate]; //error :Use of undeclared identifier appDelegate
}
Upvotes: 0
Views: 3404
Reputation: 52538
Pretty bad style: Instance variables should always start with an underscore. For example, _appDelegate. Inside an instance method, using the name of an instance variable automatically refers to self->. For example, writing "appDelegate" when you have an instance variable "appDelegate" actually means self->appDelegate. That's why you have the warning: Introducing a variable named appDelegate means that using "appDelegate" in your source code now refers to the local variable and not the instance variable anymore. That is asking for trouble. ("Asking to trouble" means "Any experienced programmer will tell you that this will lead sooner or later to a bug that is impossible to fix"). Just by adding a variable you have possibly changed the meaning of many lines of code. Prefixing all instance variables with an underscore solves this.
And that is exactly why the compiler warns you: The compiler figured out that you just dug yourself a hole that you will eventually fall into.
It is also quite strange that you have an instance variable named appDelegate or _appDelegate, because you should get the appDelegate by calling [[UIApplication sharedApplication]delegate].
Upvotes: 0
Reputation: 21
From the posted code that you had given under Edit, it seems that your problem is you just declared AppDelegate *appdelegate; in the .h
and used "appDelegate" instead of "appdelegate" in the .m.
its clearly an undefined variable, isn't it ?!
Upvotes: 1
Reputation: 854
Or you can create AppDelegate object in plist file, then you can used it in every controller...
Upvotes: 0
Reputation: 5164
in Appdelegate make method.
+(AppDelegate*)sharedInstance
{
return (AppDelegate*)[[UIApplication sharedApplication] delegate];
}
then just import appdelegate in your controller header file and use
[AppDelegate sharedInstance]. name = [NSString stringwithFormat:@"%@",ename];;
maybe this will help you.
Upvotes: 1
Reputation: 1328
For that, create one method in the class in which you want to use AppDelegate
's object,
-(AppDelegate *)appdelegate
{
(AppDelegate *)[[UIApplication sharedApplication]delegate];
}
And then, wherever you want to use object of AppDelegate
in that class, you can use it like, [self appdelegate].name
.
Upvotes: 0
Reputation: 6176
if your problem is just the warning, change the local name of the pointer to your appDelegate:
AppDelegate *myLocalPointerToAppDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
myLocalPointerToAppDelegate.name=[NSString stringwithFormat:@"%@",ename];
Upvotes: 0