单例模式(页面间传值)

单例模式(页面间传值)

 

单例模式的意思就是只有一个实例。单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。这个类称为单例类。

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值