iphone 设置全局变量的几种方法
2015-03-20 20:48
603 查看
一般的软件 都有一个 登陆的功能,然后根据登陆的UserID 查看不同的信息,这也就意味着UserID 是一个 全局变量,之后的类我们都需要用到,所以 这也就需要定义 全局变量来解决问题,这里提供2种方法:
第一种
把全局变量设置到AppDelegate中
使用很简单,在AppDelegate.h文件中 写入你需要的变量,在AppDelegate.m中加入
@synthesize +你刚刚在 .h中加入的变量
需要使用的时候倒入 头文件
添加以下代码:
AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
delegate.你的变量=YES;
第二种:(转自 http://nice.iteye.com/blog/855839 感谢ncie)
需要使用的时候 添加以下代码:
[MySingleton sharedSingleton].testGlobal = @"test";
第一种
把全局变量设置到AppDelegate中
使用很简单,在AppDelegate.h文件中 写入你需要的变量,在AppDelegate.m中加入
@synthesize +你刚刚在 .h中加入的变量
需要使用的时候倒入 头文件
添加以下代码:
AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
delegate.你的变量=YES;
第二种:(转自 http://nice.iteye.com/blog/855839 感谢ncie)
interface MySingleton : NSObject { ⇒① NSString *testGlobal; } + (MySingleton *)sharedSingleton; ⇒②@property (nonxxxx,retain) NSString *testGlobal; @end @implementation MySingleton ⇒③@synthesize testGlobal; + (MySingleton *)sharedSingleton { static MySingleton *sharedSingleton; @synchronized(self) { if (!sharedSingleton) sharedSingleton = [[MySingleton alloc] init]; return sharedSingleton; } } @end
需要使用的时候 添加以下代码:
[MySingleton sharedSingleton].testGlobal = @"test";
相关文章推荐
- 在iPhone开发中,全局变量的几种使用方法
- 在iPhone开发中,全局变量的几种使用方法
- 在iPhone开发中,全局变量的几种使用方法
- C++中设置全局变量的几种方法
- 在iPhone开发中,全局变量的几种使用方法
- 在 iPhone 开发中,全局变量的几种使用方法
- 在iPhone开发中,全局变量的几种使用方法
- C/C++定义全局变量/常量几种方法的区别
- 设置和读取ASP.Net全局变量的方法
- ASP.Net全局变量的设置和读取方法
- 定义C/C++全局变量/常量几种方法的区别
- asp.net全局变量的设置和读取方法
- 定义c/c++全局变量/常量几种方法的区别
- 定义C/C++全局变量/常量几种方法的区别
- ASP.Net全局变量的设置和读取方法
- ASP.Net全局变量的设置和读取方法
- ASP.Net下两种全局变量的设置和读取方法
- 定义C/C++全局变量/常量几种方法的区别
- C/C++定义全局变量/常量几种方法的区别
- ASP.Net全局变量的设置和读取方法