我对使用应用程序委托(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/