我试图在我的appDelegate中添加两个变量,以便可以在其他视图控制器中使用它们。
我的代码如下所示:
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;
...
但是,当在我的视图控制器中使用myappdelegate时,找不到新变量:
...
mainDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
...
mainDelegate.firstVariable = localVariable.text;
...
我的错误在哪里?
谢谢大家。
如果您的实例变量和@synthesize语句摆脱了。编译器将为您完成这项工作。
确保您#import AppDelegate.h
需要的每个位置,尤其是在MyViewController1.m和MyViewController2.m中。
我的猜测是您没有在视图控制器的.m文件中导入AppDelegate.h文件。
正如@Abizern所说,使用应用程序委托传递数据不是一个好的设计模式。您应该使您的应用程序委托保持简单,仅响应委托消息和通知。
最好创建一个数据容器单例,并使用它在项目中的对象之间共享数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句