Cocoa开发中, 如何用全局变量
比如在tabbar的开发中,可以某个页面的数据需要在back到此页面后依然有效。
可以用 appDelegate 这个对象来创建你的全局数据
这个内建的对象,在 APP 启动时产生,在 APP 退出时销毁
下面是实例
假设我们给这个变量定义的名字为 GlobalValue
在 IphoneViewAppDelegate.h 中加入下面的代码(加在 interface 外)
// 记录当次运行实例中 WebView 当前动作
@property (nonatomic, retain) NSString *GlobalValue;在 IphoneViewAppDelegate.m 文件的前面加入下面的代码
// 记录当次运行实例中 WebView 当前动作
@synthesize GlobalValue;在 IphoneViewController.m 文件的 - (void)viewDidLoad 方法中加入下面的代码
// 引入全局变量
IphoneViewAppDelegate appDelegate = (IphoneViewAppDelegate)[[UIApplication sharedApplication] delegate];
// 对变量写入
appDelegate.GlobalValue = @"loading";
在你的文件 *.m 任意一个地方,都可以通过
IphoneViewAppDelegate *appDelegate = (IphoneViewAppDelegate)[[UIApplication sharedApplication] delegate];//这种写法会报警告,那么应当修改为直接调用
(IphoneViewAppDelegate *)[[UIApplication sharedApplication] delegate]。。。。
来获得这个全局的对象
然后可以对 appDelegate.GlobalValue 进行读写
在切换界面的过程中,也能读写这个变量,这个值会在退出 APP 时自动销毁
例子:
#import <UIKit/UIKit.h>
@class KidsViewController;
@interface KidsAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) KidsViewController *viewController;
@property (strong, nonatomic) IBOutlet UINavigationController *navController;
@property (strong, nonatomic) NSMutableArray *classList;
@property (strong, nonatomic) NSMutableArray *childList;
@property (nonatomic) int currentType;
@property (nonatomic, strong) NSString *currentSelectName;
@end
@implementation KidsAppDelegate
@synthesize classList = _classList;
@synthesize childList = _childList;
@synthesize currentType = _currentType;
@synthesize currentSelectName = _currentSelectName;
- (void)dealloc
{
[_window release];
[_currentSelectName release];
[_viewController release];
[_classList release];
[_childList release];
[super dealloc];
}
赋值和调用
- (void)getChildList:(NSMutableArray *)array
{
if (array) {
self.childArray = array;
((KidsAppDelegate*)[[UIApplication sharedApplication]delegate]).childList = self.childArray;
}
}
self.currentType = ((KidsAppDelegate*)[[UIApplication sharedApplication]delegate]).currentType;
有几种方法
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;
Cocoa开发中, 如何用全局变量的更多相关文章
- ios开发中全局变量设置和调用方法
ios开发中,全局变量设置和调用方法如下:在AppDelegate.h文件中设置全局变量:@interface ***AppDelegate{NSString *myName;}@property ( ...
- XCode和Cocoa在开发中使用第三方dylib示例
XCode和Cocoa在开发中使用第三方dylib示例 www.educity.cn 发布者:yukowang 来源:网络转载 发布日期:2014年06月13日 XCode和Co ...
- iOS开发中静态库制作 之.a静态库制作及使用篇
iOS开发中静态库之".a静态库"的制作及使用篇 一.库的简介 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的类型? 根据源代码的公开情况,库可以分为2种类 ...
- iOS项目开发中的知识点与问题收集整理①(Part 一)
前言部分 注:本文并非绝对原创 大部分内容摘自 http://blog.csdn.net/hengshujiyi/article/details/20943045 文中有些方法可能已过时并不适用于现在 ...
- AngularJS移动开发中的各种坑
捂脸,辛酸泪ing...... 本文主要涉及部分在移动设备上特有的问题. 相对来说,Jquery侧重DOM操作,AngularJS是以视图模型和双向绑定为核心的. DOM操作的问题 避免使用 jQue ...
- [Unity游戏开发]向量在游戏开发中的应用(三)
本文已同步发表在CSDN:http://blog.csdn.net/wenxin2011/article/details/51088236 在上一篇博客中讲了利用向量点乘在游戏开发中应用的几种情景.本 ...
- Web 开发中应用 HTML5 技术的10个实例教程
HTML5 作为下一代网站开发技术,无论你是一个 Web 开发人员或者想探索新的平台的游戏开发者,都值得去研究.借助尖端功能,技术和 API,HTML5 允许你创建响应性.创新性.互动性以及令人惊叹的 ...
- iOS开发中的4种数据持久化方式【一、属性列表与归档解档】
iOS中的永久存储,也就是在关机重新启动设备,或者关闭应用时,不会丢失数据.在实际开发应用时,往往需要持久存储数据的,这样用户才能在对应用进行操作后,再次启动能看到自己更改的结果与痕迹.ios开发中, ...
- 二十八、带给我们一种新的编码思路——EFW框架CS系统开发中的MVC模式探讨
回<[开源]EFW框架系列文章索引> EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dADO0 EFW框架实例源代码下载:http://p ...
随机推荐
- POJ-2251-地下城
这题是一道简单的广搜题目,读入的时候,需要注意,如果是用scanf读入的话,就直接读取每行的字符串,不然的话,行尾的回车,也会被当成字符读入,这样的话,每次读取的数目就会小于我们想要的数目,因为每次把 ...
- A Fast and Easy to Use AES Library
http://www.codeproject.com/Articles/57478/A-Fast-and-Easy-to-Use-AES-Library Introduction EfAesLib i ...
- 项目之socket
客户端socket 客户端套接字完成的任务很统一,发送请求,接收请求结果 可以封装成一个方法 使用的tcp协议存在粘包问题,故需要自定义报头 import json import struct #项目 ...
- PAT Basic 1024
1024 科学计数法 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分只有1 ...
- skkyk:题解 洛谷P3865 【【模板】ST表】
我不会ST表 智推推到这个题 发现标签中居然有线段树..? 于是贸然来了一发线段树 众所周知,线段树的查询是log(n)的 题目中"请注意最大数据时限只有0.8s,数据强度不低,请务必保证你 ...
- idea中代码费格式化 ctrl+alt+L
idea中代码费格式化 ctrl+alt+L
- 4A. Just a Hook
4A. Just a Hook Time Limit: 2000ms Case Time Limit: 2000ms Memory Limit: 32768KB 64-bit integer IO ...
- php 注册与登录
<body background="timg.jpg"><div class="dak"> <div class="zu ...
- PIL:Python Imaging Library(图像处理标准库)和Qrcode:二维码生成
安装PIL Mac或Linux安装命令:sudo easy_install PIL 如果报错:fatal error: 'freetype/fterrors.h' file not found Mac ...
- 【Luogu】2114起床困难综合征(位运算贪心)
题目链接 这题真是恶心死我了. 由于位运算每一位互不干涉,所以贪心由大到小选择每一位最优的解,但是要判断一下边界,如果选择该解使得原数>m则不能选择. 代码如下 #include<cstd ...