我有一个在Appdelegate中定义的全局变量,我想在其他控制器中使用。 我可以用这样的:全球声明AppDelegate
AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
appdelegate.name=[NSString stringwithFormat:@"%@",ename];
但无论我要访问的viewController appdelegates可变我必须使用每个这就给像警告消息时AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
“AppDelgate局部声明隐藏实例变量”。所以是有一种方式,我可以声明它只有一次任何访问它在ViewController多次。我可以摆脱这种警告?
编辑:
.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
}
只是声明AppDelegate * appDelegate;在.h文件中。然后在viewdidload中写下appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];所以你可以在整个文件中访问appDelegate。 –
当我在viewDidLoad中使用它时,我得到了使用未声明的标识符appDelegate错误 – Sindhia
如果你看到那个警告,那么你的代码有一些严重错误。请发布相关代码的相关位(以及声明AppDelegate的头文件)。 – trojanfoe