kikoso
kikoso

Reputation: 708

Access App Delegate information from a different .xib file

I have one .xib file with an App Delegate. In this App Delegate, I have certain instances of variables:

//The model objects
NSString* _stringWord;
NSMutableArray *_images;
int _numberOfWordsTotal;
int _numberOfWords;

Which are conveniently declared as properties:

@property (nonatomic, copy) NSString * stringWord;
@property (nonatomic) int numberOfWordsTotal;
@property (nonatomic) int numberOfWords;
@property (nonatomic, copy) NSMutableArray * images;

And synthesized in the .m file

@synthesize stringWord = _stringWord;
@synthesize images = _images;
@synthesize numberOfWordsTotal = _numberOfWordsTotal;
@synthesize numberOfWords = _numberOfWords;

In the App Delegate I do perform some operations with this variables. More specifically, I assign some images to the _images variable. The information is correctly stored, according to the debugger.

In a different .xib file, I want to access the content of this delegate. So my idea is to access the entire delegate through the use of this function:

Visual_PoetryAppDelegate *delegate = [[UIApplication sharedApplication] delegate];

I can see that the delegate variable is instantiated, including access to all the variables. However, when it comes to deal with them, the variables are empty (the value generated in the previous .xib doesn't seem to be stored).

Any idea or suggestion? Is there any better way to access a previous delegate from a different .xib file?

Upvotes: 0

Views: 347

Answers (2)

oriolpons
oriolpons

Reputation: 1883

Change copy to retain

@property (nonatomic, retain) NSString * stringWord;
@property (nonatomic) int numberOfWordsTotal;
@property (nonatomic) int numberOfWords;
@property (nonatomic, retain) NSMutableArray * images;

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299395

The app delegate is the not the central storage place for global variables. Move this data into a singleton model object (by the looks of it, it would be something like Poem). You can access [Poem sharedPoem] from any object that needs it. Alternately, you can instantiate a single Poem object in the app delegate and hand it to the various view controllers that require it. Each approach has its advantages.

See the following questions for more discussion on correct use of the app delegate and model objects:

Upvotes: 3

Related Questions