xcode 使用 AppDelegate 使变量可用于整个应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14291244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:30:20  来源:igfitidea点击:

Make variable available to entire application using AppDelegate

iphoneiosobjective-ccxcode

提问by mandy b

I am extraordinarily confused by the concept of using the app delegate to make a variable be able to be set and used anywhere in my application. Here's my code:

我对使用应用程序委托使变量能够在我的应用程序中的任何地方设置和使用的概念感到非常困惑。这是我的代码:

appdelegate.h

appdelegate.h

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

appdelegate.m

appdelegate.m

@synthesize globalUsername;

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

和另一个视图场景(称为“Forum.h”,我试图在其中设置值)

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

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

最后一行当然是不完整的,但是 xcode 会自动选取 globalUsername 变量,我只是无法调用“appendString”函数来实际更改它。

回答by Zhang

Erm, if you're trying to store a username, why not just use NSUserDefaults ?

嗯,如果你想存储一个用户名,为什么不直接使用 NSUserDefaults 呢?

// set the username
[[NSUserDefaults standardUserDefaults] setValue:@"johnsmith" forKey:@"username"];
[[NSUserDefaults standardUserDefaults] synchronize];

// retrieve the user name
NSString *username = [[NSUserDefaults standardUserDefaults] valueForKey:@"username"];

回答by Cocoanetics

You are missing one set of [], those are actually two statements in one. first the method globalUsername is called on appDelegate and the result of that is then send the message appendString (which is missing parameters)

您缺少一组 [],它们实际上是两个语句合二为一。首先在 appDelegate 上调用 globalUsername 方法,然后发送消息 appendString (缺少参数)

So this should look like this instead:

所以这应该看起来像这样:

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

But for a game you should not abuse the app delegate like this. Rather I recommend that if you want a global place to store game state you should look into having a Singleton, like I described here: http://www.cocoanetics.com/2009/05/the-death-of-global-variables/

但是对于游戏,您不应该像这样滥用应用程序委托。相反,我建议如果您想要一个全局位置来存储游戏状态,您应该考虑使用单例,就像我在这里描述的那样:http: //www.cocoanetics.com/2009/05/the-death-of-global-variables /

PS: you probably don't want to work with a mutable string there because of several reasons. Better to have the property/ivar a normal NSString and then just set it. I don't see any good reason why you would want to append characters to a user's name. Even though NSStrings are immutable you can simply replace them with other ones.

PS:由于多种原因,您可能不想在那里使用可变字符串。最好让属性/ivar 是一个普通的 NSString,然后设置它。我看不出您为什么要在用户名后附加字符的任何充分理由。即使 NSStrings 是不可变的,您也可以简单地将它们替换为其他的。

[[appDelegate setGlobalUsername:@"foobar"];

or with same effect:

或具有相同的效果:

appDelegate.globalUsername = @"foobar";

回答by MS.

If it is NSMutableString, Then you have alloc - init in 'Delegate' class.

如果它是 NSMutableString,那么你在'Delegate' 类中有 alloc - init。

Because if it is Mutable Object, then you have to alloc & init.

因为如果是Mutable Object,那你就得alloc & init。

First, In Delegate.m class

首先,在 Delegate.m 类中

globalUserName = [[NSMutableString alloc] init];

Then,

然后,

In ViewController, you can use,

在 ViewController 中,您可以使用,

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

Thanks.

谢谢。

回答by TheEnigma2112

I think you've got the relationship between the delegate and its view controllers mixed up. The AppDelegate gets created when you launch the app. Rather than create the AppDelegate in Forum.h, you create the ViewController, Forum, in the AppDelegate. Also the *globalUserName in the curly braces is unnecessary.

我认为您已经混淆了委托与其视图控制器之间的关系。AppDelegate 在您启动应用程序时创建。不是在 Forum.h 中创建 AppDelegate,而是在 AppDelegate 中创建 ViewController、Forum。花括号中的 *globalUserName 也是不必要的。

So AppDelegate.h would read:

所以 AppDelegate.h 会读到:

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) NSMutableString *globalUsername;
@property (nonatomic, retain) Forum *forum;

Also, in the current version of Xcode and Objective-C, synthesize is also unnecessary. It gets set up and initialized automatically. In AppDelegate.m, you can refer to *globalUsername using:

此外,在当前版本的 Xcode 和 Objective-C 中,合成也是不必要的。它会自动设置和初始化。在 AppDelegate.m 中,您可以使用以下方式引用 *globalUsername:

self.globalUsername

Your AppDelegate.m should also include the following in applicationDidLaunch:

您的 AppDelegate.m 还应在 applicationDidLaunch 中包含以下内容:

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

But the Forum ViewController currently doesn't have a variable named delegate, so we'll have to create that. So in Forum.h you should add:

但是 Forum ViewController 当前没有一个名为 delegate 的变量,所以我们必须创建它。所以在 Forum.h 你应该添加:

@property (nonatomic) id delegate;

So now in Forum.m, since AppDelegate.m took care of setting the delegate, we can now refer to the data in AppDelegate using Forum's delegate variable.

所以现在在 Forum.m 中,由于 AppDelegate.m 负责设置委托,我们现在可以使用 Forum 的委托变量引用 AppDelegate 中的数据。

So finally, in order to access globalUsername from Forum.m, we can use self.delegate.globalUsername. So to append to the string from Forum.m, we can use:

所以最后,为了从 Forum.m 访问 globalUsername,我们可以使用 self.delegate.globalUsername。因此,要附加到来自 Forum.m 的字符串,我们可以使用:

[self.delegate.globalUsername appendString:myNewString];

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

其中“myNewString”是您要添加到 globalUsername 的字符串。

Hope this helps.

希望这可以帮助。

回答by iKambad

I think you forgot to allocit. Just allocit once in application did finish launchingmethod.

我想你忘记了alloc。只alloc用一次application did finish launching方法。

回答by Anoop Vaidya

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

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

This doesnot create a global variable. Infact it is a protected member for AppDelegate Class.

这不会创建全局变量。事实上,它是 AppDelegate 类的受保护成员。

From all the classes you cant access it. You need to create yet another instance of AppDelegate and then you will wise that ivar.

您无法从所有课程中访问它。您需要创建 AppDelegate 的另一个实例,然后您将明智地使用该 ivar。

The term global refers that you will be having same value througout the application in all the classes. And you not need to alloc+init everytime.

术语全局是指您将在所有类中的整个应用程序中具有相同的值。而且你不需要每次都分配+初始化。

For Global Variable you need to create a static variable. A shared class / Singleton class will work for you.

对于全局变量,您需要创建一个静态变量。共享类/单例类对您有用。