Make the variable available to the entire application using AppDelegate

I am extremely confused by the concept of using an application delegate so that I can set and use a variable anywhere in my application. Here is my code:

appdelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate> { NSMutableString *globalUsername; } @property (strong, nonatomic) UIWindow *window; @property (nonatomic, retain) NSMutableString *globalUsername; 

appdelegate.m

 @synthesize globalUsername; 

and another viewing scene (called "Forum.h", where I am trying to set the value)

 AppDelegate *appDelegate = (MyAppDelegate *)[[[UIApplication sharedApplication] delegate]]; [appDelegate globalUsername appendString; 

the last line, of course, is incomplete, but xcode automatically pickets the variable globalUsername, I just can not call the function "appendString" to really change it.

+4
source share
6 answers

Erm, if you are trying to save a username, why not just use NSUserDefaults?

 // set the username [[NSUserDefaults standardUserDefaults] setValue:@"johnsmith" forKey:@"username"]; [[NSUserDefaults standardUserDefaults] synchronize]; // retrieve the user name NSString *username = [[NSUserDefaults standardUserDefaults] valueForKey:@"username"]; 
+11
source

You are missing one set of [], in fact, these are two operators in one. first, the globalUsername method is called in appDelegate, and the result of this message then sends an appendString message (in which there are no parameters)

So it should look like this:

 [[appDelegate globalUsername] appendString:@"foobar"]; 

But for the game you should not abuse the application delegate like this. Rather, I recommend that if you want to create a global place to store game state, you should study singleton, as I described here: http://www.cocoanetics.com/2009/05/the-death-of-global-variables /

PS: you probably don't want to work with mutable string due to several reasons. It is better to have the / ivar property of a regular NSString, and then just set it. I see no good reason why you want to add characters to the username. Although NSStrings are immutable, you can simply replace them with others.

 [[appDelegate setGlobalUsername:@"foobar"]; 

or with the same effect:

 appDelegate.globalUsername = @"foobar"; 
+6
source

If it is an NSMutableString, then you have alloc - init in the class 'Delegate'.

Because if it is a Mutable Object, you need to highlight init as well.

Firstly, in the class Delegate.m

 globalUserName = [[NSMutableString alloc] init]; 

Then

In ViewController you can use

 appDelegate.globalUsername appendString ....(What you want) 

Thanks.

+4
source

I think you messed up the relationship between the delegate and the controllers of his views. AppDelegate is created when the application starts. Instead of creating an AppDelegate in Forum.h, you create a ViewController, Forum, in AppDelegate. Also * globalUserName in braces is not required.

So AppDelegate.h will read:

 @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (nonatomic, retain) NSMutableString *globalUsername; @property (nonatomic, retain) Forum *forum; 

In addition, the current version of Xcode and Objective-C do not need to be synthesized either. It is automatically configured and initialized. In AppDelegate.m, you can reference * globalUsername using:

 self.globalUsername 

The following applicationDidLaunch application should also be included in AppDelegate.m:

 [[self.forum alloc] init]; //Allocate and initialize the forum ViewController self.forum.delegate = self; //Set the forum ViewController delegate variable to be the AppDelegate itself 

But there is currently no variable called delegate in the Forum ViewController, so we will have to create it. So, in Forum.h you should add:

 @property (nonatomic) id delegate; 

So now in Forum.m, since AppDelegate.m took care of setting up the delegate, now we can reference the data in AppDelegate using the Forum delegate variable.

So, to access globalUsername from Forum.m, we can use self.delegate.globalUsername. So, to add a line from Forum.m, we can use:

 [self.delegate.globalUsername appendString:myNewString]; 

Where "myNewString" is the string you want to add to the globalUsername name.

Hope this helps.

+2
source

I think you forgot its alloc . Just alloc it once in the application did finish launching .

0
source

@interface AppDelegate : UIResponder <UIApplicationDelegate> { NSMutableString *globalUsername; }

This does not create a global variable. Infact is a protected member of the AppDelegate class.

Of all the classes that you cannot access. You need to create another instance of AppDelegate, and then you will be wise that ivar.

The term global means that you will have the same meaning for the application in all classes. And you do not need to allocate + init every time.

For a global variable, you need to create a static variable. The shared / Singleton class will work for you.

0
source

All Articles