gpt4 book ai didi

iphone - 使用 AppDelegate 使变量对整个应用程序可用

转载 作者:可可西里 更新时间:2023-11-01 05:01:01 25 4
gpt4 key购买 nike

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

应用委托(delegate).h

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

appdelegate.m

@synthesize globalUsername;

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

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

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

最佳答案

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

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

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

关于iphone - 使用 AppDelegate 使变量对整个应用程序可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14291244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com