开发者

Error While referencing AppDelegate

开发者 https://www.devze.com 2023-02-18 07:53 出处:网络
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

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 = (iSolveM开发者_运维问答athAppDelegate *)[[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


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]; 

    ...
}


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消