tonytran
tonytran

Reputation: 73

change value of var in AppDelegate

In my AppDelegate, I have

#import <UIKit/UIKit.h>
#import "CustomerProfile.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property   (strong, nonatomic) UIWindow *window;
@property   (strong, nonatomic) int x;

@end

At class B, I do

AppDelegate *appDelegate        =   [[UIApplication  sharedApplication] delegate];
appDelegate.x   =   5;

Then at Class C, I do

 AppDelegate *appDelegate        =   [[UIApplication  sharedApplication] delegate];
 appDelegate.x   =   4;

Eventually, at class D I print out the result of x and x = 5. Should x be 4. It is confusing me. Please advice me on this issue. Thanks

Upvotes: 3

Views: 2288

Answers (1)

MarioGT
MarioGT

Reputation: 682

In your App delegate method your property x is set to strong (aka retain), you have to set to assign, a int var can't be retained because its not a object:

@property (assign, nonatomic, readwrite) int x; //then @synthesize in the implementation

Second, you have to import the header of your appDelegate (in your B,C,D Classes)

#import "yourAppDelegate.h" 

set your appDelegate instance:

yourAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate]; // or [NSApplication sharedApplication] if your app it is for OS X

then set your x var to the desired value

appDelegate.x = 5 (or whatever)

I tested this in one of my projects and works.

Upvotes: 6

Related Questions