iOS应用程序中的dynamic全局variables

我search了互联网和20多个关于全局variables的话题,我找不到我需要的。

  • 将在iOS应用程序中使用它。
  • 我希望variables可以从所有视图访问。
  • 我想要一些意见能够改变这个variables。
  • 最好不要惹麻烦。

你可以做什么build议,在AppDelegate中添加一个variables,然后从你的视图控制器中获取它像这样:

AppDelegateNeme *ap = (AppDelegateNeme *)[[UIApplication sharedApplication] delegate]; ap.yourVar = smth 

正如你在上面的代码中看到的,你正在访问UIApplication的共享实例,只要将它转换到你的AppDelegate类,你就可以到达你的“全局”variables。 但我个人并不认为这是最好的解决scheme。 会发生什么是你需要在每个UIViewController中包含你的应用程序委托,而且你可能已经在你的AppDelegate中包含了这个UIViewController,所以你最终可能会看到recursion包含或者非常混乱的代码。

更清洁的方法将创build一个类来存储您的全局variables,只需添加一个文件,并为类selectNSObject。 然后你可以创build该类的单例对象,或者你可以定义类variables和类方法(带有+的方法)来存储和取自“全局variables”的值。

通过这种方式,你的代码将是模式可读的,只要你不开始在这个类中包含东西,你将永远不会有任何包含问题。

例:

 //GlobalClass.h @interface GlobalClass : NSObject @property (nonatomic) BOOL someBool; // example wit a singleton obj: + (GlobalClass *)globalClass; // example with class methods +(int)GetMyVar; +(void)SetMyVar:(int)var; //GlobalClass.m static int MyVar; @synthesize someBool; static GlobalClass *globalClass = nil; + (GlobalClass *)globalClass { if (globalClass == NULL) { // Thread safe allocation and initialization -> singletone object static dispatch_once_t pred; dispatch_once(&pred, ^{ globalClass = [[GlobalClass alloc] init]; }); } return globalClass; } +(int)GetMyVar { return MyVar; } +(void)SetMyVar:(int)var { MyVar = var; } 

所以从外面(从你的viewcontroller):要创build单身人士,并设置我们的布尔属性:

 //set var: [[GlobalClass globalClass] setSomeBool:YES]; // get BOOL b = [[GlobalClass globalClass] someBool]; 

或者使用类方法(不需要创buildsingletone obj)

 // set [GlobalClass SetMyVar:5]; // get int num = [GlobalClass GetMyVar]; 

希望这可以帮助…

在应用程序委托头

 extern NSString* const globalVariable1; 

在应用程序委托m文件

 NSString* const globalVariable1 = @"My Value"; 

你可以在任何视图控制器中访问这个全局variables。

这只是为了string。

int const

只是BOOL布尔值。

希望这可以帮助

对于swift我用这个方法:

 struct MyStyles { static let ThemeColor = UIColor(red: 41/255, green: 156/255, blue: 253/255, alpha: 1.0) static let ThemeSecondColor = UIColor.whiteColor() static let ThemeGrayLineColor = UIColor(red: 128/255, green: 128/255, blue: 128/255, alpha: 1.0) static let ThemeMyCommentsColor = UIColor(red: 219/255, green: 238/255, blue: 255/255, alpha: 1.0) static let ThemeCommentedToMeColor = UIColor(red: 227/255, green: 232/255, blue: 235/255, alpha: 1.0) static let ThemeFontName = "HelveticaNeue-Light" static let ThemeFontNameBold = "Helvetica-Bold" static let ScreenWidth = UIScreen.mainScreen().bounds.size.width static let ScreenHeight = UIScreen.mainScreen().bounds.size.height static let StatusBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height static func largeButton(#title: String, action: String, target: UIViewController) -> UIButton { let button = UIButton(frame: CGRect(x: 0, y: ScreenHeight*9/10, width: ScreenWidth, height: ScreenHeight/10)) button.setTitle(title, forState: UIControlState.Normal) button.backgroundColor = ThemeSecondColor button.tintColor = ThemeColor button.titleLabel?.font = UIFont(name: ThemeFontName, size: 50) button.setTitleColor(ThemeColor, forState: UIControlState.Normal) button.addTarget(target, action: Selector(action), forControlEvents: UIControlEvents.TouchUpInside) return button } } 

我从其他类中像这样访问它们:

 var color = MyStyles.ThemeColor 

你应该在你的Application Delegate类中声明你的“全局”variables。

这样你可以确保你所有的viewsControllers等都可以访问它们。

对我来说,这可能是最优雅的解决scheme,当你需要访问这些variables之一时,你可以获得对你的应用程序委托的引用。

创build一个单例类。 将所有全局variables添加为读/写属性。 创build应用程序启动时的单例实例,通常在application:didFinishLaunchingWithOptions:方法中。 使用这个单例实例,您现在可以在整个应用程序中共享信息。

如果您的应用程序将在multithreading环境中访问这些属性,则可能需要使属性线程安全 。

希望有所帮助!