引用 AppDelegate 时出错

发布于 2024-10-25 02:58:05 字数 2406 浏览 10 评论 0原文

我的项目中有一个应用程序委托和 3 个视图控制器。我的应用程序委托中有一个变量(NSMutable 数组),我想从视图控制器访问它。因此,我决定创建一个指向我的应用程序委托的指针并访问变量。 这是我的代码:

iSolveMathAppDelegate.h

#import <UIKit/UIKit.h>

@interface iSolveMathAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UITabBarController *tabBarController;
    NSMutableArray *tmpArray;
    }


@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) NSMutableArray *tmpArray; // variable I want to access
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

iSolveMathAppDelegate.m

#import "iSolveMathAppDelegate.h"


@implementation iSolveMathAppDelegate

@synthesize window;
@synthesize tabBarController;
@synthesize tmpArray;
...
- (void)dealloc {
    [tabBarController release];
    [window release];
    [tmpArray release];
    [super dealloc];
}


@end

我想从中访问 tmpArray 的视图控制器类。

referenceViewController.h

#import <UIKit/UIKit.h>

@class iSolveMathAppDelegate;

@interface referenceViewController : UITableViewController {
    NSMutableArray *equationTypes;
    iSolveMathAppDelegate *data;

}

@property(nonatomic, retain) NSMutableArray *equationTypes;
@property(nonatomic, retain) iSolveMathAppDelegate *data;

@end

最后是referenceViewController.m

#import "referenceViewController.h"


    @implementation referenceViewController
    @synthesize equationTypes, data;

     data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate]; 
//says that initializer element is not constant...ERROR!




        - (void)viewDidLoad {
            [super viewDidLoad];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"equationTemplates"ofType:@"plist"];
        data.tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
        self.equationTypes = data.tmpArray;
  [data.tmpArray release]; // obviously none of these work, as data is not set.

    }


    - (void)dealloc {
        [super dealloc];
        [equationTypes release];
        [data release];
    }


    @end

所以无论如何,在 data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate]; 行,编译器表示初始化器元素不是常量。

我在网上寻找答案,尽管它似乎有效......但对我来说没有骰子:(你能告诉我哪里出了问题吗?我正在使用 XCode 3.2 和 iOS SDK 3...... 是SDK的问题。

也许

I have an App Delegate and a 3 view controllers in my project. I have a variable(a NSMutable Array) in my App Delegate which I want to access from my view controllers. So I decided to create a pointer to my App Delegate and access the variables.
Here is my code:

iSolveMathAppDelegate.h

#import <UIKit/UIKit.h>

@interface iSolveMathAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UITabBarController *tabBarController;
    NSMutableArray *tmpArray;
    }


@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) NSMutableArray *tmpArray; // variable I want to access
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

iSolveMathAppDelegate.m

#import "iSolveMathAppDelegate.h"


@implementation iSolveMathAppDelegate

@synthesize window;
@synthesize tabBarController;
@synthesize tmpArray;
...
- (void)dealloc {
    [tabBarController release];
    [window release];
    [tmpArray release];
    [super dealloc];
}


@end

The view controller class from which I want to access the tmpArray.

referenceViewController.h

#import <UIKit/UIKit.h>

@class iSolveMathAppDelegate;

@interface referenceViewController : UITableViewController {
    NSMutableArray *equationTypes;
    iSolveMathAppDelegate *data;

}

@property(nonatomic, retain) NSMutableArray *equationTypes;
@property(nonatomic, retain) iSolveMathAppDelegate *data;

@end

And finally referenceViewController.m

#import "referenceViewController.h"


    @implementation referenceViewController
    @synthesize equationTypes, data;

     data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate]; 
//says that initializer element is not constant...ERROR!




        - (void)viewDidLoad {
            [super viewDidLoad];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"equationTemplates"ofType:@"plist"];
        data.tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
        self.equationTypes = data.tmpArray;
  [data.tmpArray release]; // obviously none of these work, as data is not set.

    }


    - (void)dealloc {
        [super dealloc];
        [equationTypes release];
        [data release];
    }


    @end

So anyway at the line data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate]; the compiler says that the initializer element is not constant.

I have scrouged the web for answers, and for all it seems to work...but no dice for me :( Can you please advice me on where I have gone wrong? I am using XCode 3.2 and iOS SDK 3....maybe the SDK is the problem.

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏拉图鍀咏恒 2024-11-01 02:58:05

该行代码不在方法或函数中,因此编译器将其视为编译时常量或静态/全局变量的定义。这些需要常量值进行初始化。

您应该将data 的赋值放在方法中。一个好地方是 -viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];

    data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    ...
}

That line of code isn't in a method or function, so the compiler is treating it as the definition of a compile-time constant or static/global variable. Those need constant values for initialization.

You should put the assignment of data within a method. A good place would be -viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];

    data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    ...
}
近箐 2024-11-01 02:58:05

我弄清楚了结构和联合的问题。我所要做的就是将我的referenceViewController.h 文件中的@class iSolveAppDelegate 更改为#import "iSolveAppDelegate.h"。感谢乔纳森的帮助!

I figured out the struct and union problem. All I had to do was change @class iSolveAppDelegate to #import "iSolveAppDelegate.h" in my referenceViewController.h file. Thanks Jonathan for your help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文