在iphone中全局访问整数和字符串?

Rah*_*yas 3 iphone xcode objective-c

我想访问一个整数和一个字符串从一个类到所有其他类?最好的方法是什么?我想在程序中的任何位置增加值,然后想要在声明的类中更新它们....

任何想法如何实现这一点???

Nik*_*uhe 5

这是关于单身人士的问题(和好的答案).

您也可以像frankodwyer建议的那样使用app delegate,并使用以下方式从任何地方访问它:

id delegate = [[UIApplication sharedApplication] delegate];
Run Code Online (Sandbox Code Playgroud)

为了便于使用和类型安全,我使用这样的类别:

// put his in your delegate header file
@interface UIApplication(MyAppAdditions)
+ (MyAppDelegate*)sharedDelegate;
@end

// put his in your delegate implementation file
@implementation UIApplication(MyAppAdditions)
+ (MyAppDelegate*)sharedDelegate {
    return (MyAppDelegate*)[[self sharedApplication] delegate];
}
@end
Run Code Online (Sandbox Code Playgroud)

现在,您可以从任何地方访问您的应用代表: [UIApplication sharedDelegate]