IOS中设置全局变量


转:http://blog.csdn.net/totogogo/article/details/7355203

 

有几种方法

some developers recommend use singleton patter (ref link http://blog.csdn.net/kmyhy/article/details/7026511)

方法1:使用静态变量 (不推荐)

方法2: 使用singleton pattern (ref link: http://nice.iteye.com/blog/855839

方法3:把全局变量设置到AppDelegate中

例: 定义和使用一个全局变量"isLogin"

AppDelegate.h

 

@interface AppDelegate :UIResponder <UIApplicationDelegate>

@property (strong,nonatomic)UIWindow *window;

@propertyBOOL isLogin;

@end

 

AppDelegate.m

 

@implementation AppDelegate

@synthesize window =_window;

@synthesize isLogin;

@end

 
那么在其他的class里,则可以通过下列代码调用全局变量

AppDelegate *delegate=(AppDelegate*)[[UIApplicationsharedApplication]delegate];

delegate.isLogin=YES;

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM