我试图将我的头围绕在单身人士身上,我明白App Delegate本质上是一个单身对象。 我正在尝试在App Delegate中有一些我可以从任何其他类访问的成员变量。 我在App Delegate中这样做了:
@interface AppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; RootViewController *viewController; int screenwidth; } @property (nonatomic, retain) UIWindow *window; @property (nonatomic) int screenwidth;然后在.m中我这样做了:
- (void) applicationDidFinishLaunching:(UIApplication*)application { ... screenwidth=400; //arbitrary test number现在我在项目中有另一个类,它在.h中执行此操作:
#import "AppDelegate.h"在.m我有这个地方:
test=(AppDelegate*)[[[UIApplication sharedApplication] delegate] screenwidth];但是,它声称“screenwidth”是一个未找到的实例方法。 我也试过这个:
test=(AppDelegate*)[[UIApplication sharedApplication] delegate].screenwidth;这使用点语法,因为screenwidth是合成的,但它声称property screenwidth not found
我确信这些是可以简单纠正的基本问题。 任何帮助赞赏。
I'm trying to wrap my head around singletons and I understand that the App Delegate is essentially a singleton object. I'm trying have some member variables in App Delegate that I can access from any other class. I did this in the App Delegate:
@interface AppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; RootViewController *viewController; int screenwidth; } @property (nonatomic, retain) UIWindow *window; @property (nonatomic) int screenwidth;Then in the .m I did this:
- (void) applicationDidFinishLaunching:(UIApplication*)application { ... screenwidth=400; //arbitrary test numberNow I have another class in the project, and it does this in the .h:
#import "AppDelegate.h"In the .m I have this somewhere:
test=(AppDelegate*)[[[UIApplication sharedApplication] delegate] screenwidth];However, it claims that "screenwidth" is an instance method that is not found. I also tried this:
test=(AppDelegate*)[[UIApplication sharedApplication] delegate].screenwidth;This uses the dot syntax since screenwidth was synthesized, but it claims that property screenwidth not found
I'm sure these are basic issues that can be corrected simply. Any help appreciated.
最满意答案
考虑尝试:
test=[(AppDelegate*)[[UIApplication sharedApplication] delegate] screenwidth];我认为你的两次尝试都试图将.screenwidth结果转换为AppDelegate* 。
Consider trying:
test=[(AppDelegate*)[[UIApplication sharedApplication] delegate] screenwidth];I think your two tries are trying to cast the .screenwidth result to an AppDelegate*.
更多推荐
发布评论