IOS开发-全局变量的设定

来源:互联网 发布:免费名片设计软件 编辑:程序博客网 时间:2024/06/11 04:44

转载请标明原出处谢谢!

http://blog.sina.com.cn/s/blog_a6bd98c90102v3jn.html

全局变量的设定需要在AppDelegete.hAppDelegate.m文件中进行变量声明。

例如,我需要在不同的文件之间传递一个链接url,那么我需要在如下几个文件添加如下代码:

在AppDelegete.h中添加:

@property NSString *weburl;

 

在Appdelegate.m中添加:

@synthesize weburl;

 

在ViewController.h中添加:

#import "AppDelegate.h"

 

在ViewController.m中添加:

AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];

self.webViewController.url delegate.weburl;

 

解说:AppDelegate.h和.m文件中的变量就是全局变量,在ViewController.h中需要包含AppDelegate.h文件已包含这个变量,在ViewController.m中即可以使用这个变量了。类似地,不仅可以调用,还可以设定。不论设定还是调用对象都是delegate中的值,也就是只要有一处进行了修改,其它调用这个变量的地方值也会跟着被修改,符合全局变量的特征。

0 0
原创粉丝点击