【发布时间】:2014-12-27 14:12:53
【问题描述】:
我正在尝试在我的 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;
...
我的错误在哪里??
谢谢大家。
【问题讨论】:
-
有很多错误。一个主要的问题是实例变量的名称与合成属性时使用的名称不同。此外,除非您的委托访问这些控制器,否则您不需要
@class行。您的视图控制器是否导入“AppDelegate.h”?你如何声明mainDelegate? -
啊!使用应用程序委托在您的视图控制器之间传递数据!你不妨在额头上纹一个牌子,上面写着“我不知道自己在做什么”。
-
那么,有错误信息吗?
-
您可能需要将 AppDelegate.h 文件导入到您的其他类中。
-
是的,我将 AppDelegate.h 导入到我的其他类中,但没有找到!
标签: ios objective-c xcode variables appdelegate