iOS 全局变量设置的几种方式~

在iOS开发过程中关于全局变量的几个方法

1.

在APPDelegate中声明并初始化全局变量.
AppDelegate可以在整个应用程序中调用,在其他页面中可以使用代码段获取AppDelegate的全局变量:AppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
因此可以在AppDelegate.h中定义需要全局使用的变量。
   /** 设置全局变量的属性. */
    @property (nonatomic, assign)BOOL isLong;
   /** 给全局变量赋值.
  • 通过单例模式获取属性
*/
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    myDelegate.isLong = YES;
  • 在viewController中获取该值
  • AppDelegate *myDelegate = [[UIApplication sharedApplication]delegate];
        myDelegate.isLong = YES;
        NSLog(@"myDelegate: %d", myDelegate.isLong);

    2.使用全局变量

猜你喜欢

转载自www.cnblogs.com/henusyj-1314/p/9587757.html