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