Reputation: 426
Suppose i have declared the NSString in appDelegate and i want to use that string in my ViewController. How can i use that string in my ViewController?
Upvotes: 0
Views: 581
Reputation: 1740
((appDelegate *)[UIApplication sharedApplication].delegate).myString;
Upvotes: 0
Reputation: 7986
For just a string, many people will recommend NSUserDefaults.
If you want to use the AppDelegate, the answer overlaps this question Call a function in AppDelegate?
Here is my answer
You can create a header file that has a macro to the AppDelegate
GlobalData.h
#import "AppDelegate.h"
#define APPDELEGATE (AppDelegate *)[[UIApplication sharedApplication] delegate]
Then use it in any class
#import "GlobalData.h"
// to gain access to the delegate
AppDelegate * appDelegate = APPDELEGATE;
To access your string (assume it is a property called globalString)
appDelegate.globalString;
I use this approach because I can then store more #define's to some global constants
(ie soundFXVolume - #define SOUND_FX_V 0.6
)
Upvotes: 3