访问AppDelegate的成员变量(Accessing member variable of AppDelegate)

编程入门 行业动态 更新时间:2024-10-18 05:50:13
访问AppDelegate的成员变量(Accessing member variable of AppDelegate)

我试图将我的头围绕在单身人士身上,我明白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 number

Now 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*.

更多推荐

本文发布于:2023-08-04 14:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   成员   AppDelegate   variable   member

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!