我想在我的appDelegate中添加两个变量,以便我可以在其他视图控制器中使用它们。在AppDelegate中添加变量Objective c
我的代码如下所示:
myappdelegate.h:
@class MyViewController1, MyViewController2; // controller than use my new variables
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSString* firstVariable;
NSString* secondVariable;
}
@property (retain) NSString* firstVariable;
@property (retain) NSString* secondVariable;
@end
在myappdelegate.m我合成的变量:
...
@synthesize firstVariable = _firstVariable;
@synthesize secondVariable = _secondVariable;
...
但是当我的ViewController使用myappdelegate不要”找到我的新变量:
...
mainDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
...
mainDelegate.firstVariable = localVariable.text;
...
我的错误在哪里?
感谢所有。
有一些错误。主要的一点是,实例变量的名称与合成属性时使用的名称不同。另外,除非委托人访问这些控制器,否则不需要'@ class'行。你的视图控制器是否导入“AppDelegate.h”?你如何声明'mainDelegate'? – 2014-12-27 14:28:22
啊!使用应用程序委托在您的视图控制器之间传递数据!你可能还会在额头上贴上一句“我不知道自己在做什么”的标志。 – Abizern 2014-12-27 14:30:05
那么,有没有错误信息? – 2014-12-27 14:32:33