单例模式(页面间传值)
2015-12-22 22:51
344 查看
单例模式(页面间传值)
单例模式的意思就是只有一个实例。单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。这个类称为单例类。
[b]1.单例模式的要点:
显然单例模式的要点有三个;一是某个类只能有一个实例;二是它必须自行创建这个实例;三是它必须自行向整个系统提供这个实例。
2.单例模式的优点:
1.实例控制:Singleton会阻止其他对象实例化其自己的 Singleton
对象的副本,从而确保所有对象都访问唯一实例。
2.灵活性:因为类控制了实例化过程,所以类可以更加灵活修改实例化过程
要使用单例在页面间传值,首先要先创建一个单例,代码如下
创建一个类存放单例
#import
<Foundation/Foundation.h>
@interface GPS :
NSObject
//声明需要传值的属性
@property(nonatomic,strong)NSString *cityName;
@property(nonatomic,strong)NSString *lng;
@property(nonatomic,strong)NSString *lat;
//构造一个单例初始化方法
+(GPS *)shareInstance;
@end
在该类的.m文件中实现单例的初始化方法
#import
"GPS.h"
@implementation GPS
static
GPS *gps = nil;
+(GPS *)shareInstance{
if(gps ==
nil){
gps = [GPS
new];
}
returngps;
}
//为了防止方法被多次调用而引发冲突,使用gcb的方式来创建单例
//+(instancetype)getInstance{
// static dispatch_once_tonceToken;
// dispatch_once(&onceToken,^{
// gps = [self new];
// });
// return gps;
//}
//如果允许另外使用alloc/new创建非单例对象,则不必重写此方法
//+(instancetype)allocWithZone:(struct_NSZone *)zone{
// //防止本方法被多次调用而引发冲突
// staticdispatch_once_t onceToken;
// dispatch_once(&onceToken,^{
// //此处代码仅会被调用一次
// gps = [super allocWithZone:zone];
// });
// returngps;
//}
//为了防止开发者通过allocnew
来构造新对象,重写allocWithZone
+(id)allocWithZone:(struct
_NSZone*)zone{
if(gps ==nil){
gps = [super
allocWithZone:zone];
}
returngps;
}
//为了防止通过copy产生新的实例对象,需重写copy方法
-(id)copyWithZone:(NSZone *)zone{
returnself;
}
//以下代码仅在非arc环境下需要
#if !__has_feature(objc_arc)
-(NSUInteger)retainCount
{
returnNSUIntegerMax;
}
-(id)retain
{
returnself;
}
-(onewayvoid)release
{
}
-(id)autorelease
{
returnself;
}
#endif
@end
二、在有数据的页面实现推送到下个页面的方法,并将输入的值传给上面那个类定义的属性,代码如下
#import
"ViewController.h"
//导入文件
#import
"AViewController.h"
#import
"GPS.h"
@interface
ViewController ()
//定义,关联属性
@property (weak,
nonatomic)IBOutlet
UITextField*cityTF;
@property (weak,
nonatomic)IBOutlet
UITextField*lngTF;
@property (weak,
nonatomic)IBOutlet
UITextField*latTF;
@end
@implementationViewController
- (void)viewDidLoad{
[super
viewDidLoad];
}
//实现切换页面
- (IBAction)pushA:(id)sender {
UIStoryboard*storyboard = [UIStoryboard
storyboardWithName:@"Main"bundle:nil];
AViewController*aViewController = [storyboard
instantiateViewControllerWithIdentifier:@"AViewController"];
[self.navigationController
pushViewController:aViewController
animated:YES];
//传值给属性
[GPS
shareInstance].cityName =
self.cityTF.text;
[GPS
shareInstance].lng =
self.lngTF.text;
[GPS
shareInstance].lat =
self.latTF.text;
}
- (void)didReceiveMemoryWarning{
[super
didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
@end
三、在需要值的页面进行取值
#import
"AViewController.h"
#import
"GPS.h"
@interface
AViewController ()
@property (weak,
nonatomic)IBOutlet
UILabel*cityLable;
@end
@implementationAViewController
- (void)viewDidLoad{
[super
viewDidLoad];
//Do any additional setup after loading the view.
self.cityLable.text=[GPS
shareInstance].cityName;
self.cityLable.textColor= [UIColor
blackColor];
}
@end
[/b]
相关文章推荐
- iOS开发-设计模式
- Java线程:堵塞队列与堵塞栈
- [javascript-debug-ajax-json]两种不同的json格式数据
- 精品】资料全+模仿手机的单片机设计+多功能应用操作系统
- arm指令集
- IOS中常见字符串的几种方法
- Ubuntu下PyDev安装
- 1. Composer 简介
- HTTP 协议格式 和 HTTP Header
- Mac系统下安装mysql
- 公钥,私钥和数字签名这样最好理解
- 数据结构 JAVA描述(一) 线性表
- iOS开发之手势识别
- LayoutInflater的认识
- 窗口刷新问题(WMPAINT、BeginPaint、EndPaint的说明)
- 深入理解Javascript面向对象编程
- 单片机汇编指令
- 心上莲花:环环相扣的世界
- apple watch 图片
- 程序猿之华丽转身:写Paper也很重要