iOS项目中全局变量的定义与使用
2017-03-15 17:07
375 查看
在一个项目中,我们可能需要定义几个全局变量,在我们程序的任何位置都可以进行访问,提高我们的开发效率。在iOS中我们如何来实现呢?我们主要使用的是AppDelegate类来实现。如下:
(1)AppDelegate.h:
[objc] view
plain copy
print?
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) NSString *myName;//声明一个全局变量;
@end
(2)ViewController.m
这个是第一个页面。
[objc] view
plain copy
print?
#import "ViewController.h"
#import "AppDelegate.h" //需要引入这个头文件;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:true];
AppDelegate *app = [[UIApplication sharedApplication] delegate];
NSLog(@"%@",app.myName);
app.myName = @"第一个页面";
}
@end
(3)SecondViewController.m
这个是第二个页面。
[objc] view
plain copy
print?
#import "SecondViewController.h"
#import "AppDelegate.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated{
AppDelegate *app = [[UIApplication sharedApplication] delegate];
NSLog(@"%@",app.myName);
app.myName = @"第二个页面";
}
@end
最后在两个页面之间跳转,输出结果如下:
。
这表示我们对同一个变量进行了操作。为什么在AppDelegate中可以声明全局变量呢?因为使用了单例,AppDelegate就是一个单例的类,实现了UIApplicationDelegate这个委托。只要我们在程序的任何地方声明了AppDelegate的对象,这个对象就是唯一的,所以就可以实现全局变量
原文地址:http://blog.csdn.net/chenyufeng1991/article/details/49282325
(1)AppDelegate.h:
[objc] view
plain copy
print?
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) NSString *myName;//声明一个全局变量;
@end
(2)ViewController.m
这个是第一个页面。
[objc] view
plain copy
print?
#import "ViewController.h"
#import "AppDelegate.h" //需要引入这个头文件;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:true];
AppDelegate *app = [[UIApplication sharedApplication] delegate];
NSLog(@"%@",app.myName);
app.myName = @"第一个页面";
}
@end
(3)SecondViewController.m
这个是第二个页面。
[objc] view
plain copy
print?
#import "SecondViewController.h"
#import "AppDelegate.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated{
AppDelegate *app = [[UIApplication sharedApplication] delegate];
NSLog(@"%@",app.myName);
app.myName = @"第二个页面";
}
@end
最后在两个页面之间跳转,输出结果如下:
。
这表示我们对同一个变量进行了操作。为什么在AppDelegate中可以声明全局变量呢?因为使用了单例,AppDelegate就是一个单例的类,实现了UIApplicationDelegate这个委托。只要我们在程序的任何地方声明了AppDelegate的对象,这个对象就是唯一的,所以就可以实现全局变量
原文地址:http://blog.csdn.net/chenyufeng1991/article/details/49282325
相关文章推荐
- iOS 使用const定义项目全局变量
- iOS项目中全局变量的定义与使用
- ios 全局变量的 定义使用
- ios开发 全局变量的定义和使用
- IOS学习之ios全局变量定义和使用
- ios开发(十): 全局变量的定义和使用
- MFC中全局变量的定义及使用
- 5章6节局部与全局使用同一个变量不同定义的调用
- 全局变量的定义和使用
- Android之项目全局变量的定义
- MFC中全局变量的定义及使用
- 全局变量的定义以及函数内怎么使用局部变量和全局变量?
- MFC中全局变量的定义及使用
- 使用配置类定义 Codeigniter 全局变量
- Android全局变量的定义与使用
- CodeIgniter 定义“全局变量-global variable”,可以在所有controller,model和view中使用
- ios全局变量使用
- 100914 项目中把全局变量放到一个类中定义的方法
- asp.Net项目中全局变量使用的方式
- extern全局变量的定义使用