objective-c – 在iphone中全局访问整数和字符串?

前端之家收集整理的这篇文章主要介绍了objective-c – 在iphone中全局访问整数和字符串?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想访问一个整数和一个字符串从一个类到所有其他类?
最好的方法是什么?
我想在程序中的任何位置增加值,然后想要在声明的类中更新它们….

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

解决方法

关于 singletons,这是一个问题(和好的答案).

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

id delegate = [[UIApplication sharedApplication] delegate];

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

// 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

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

猜你在找的Xcode相关文章