IOS 单例的简单创建和使用


新建一个InforCenter类------------单例文件

InforCenter.h文件

#import <Foundation/Foundation.h>


@interface InforCenter : NSObject

@property (nonatomic,strong)NSString * userName;

@property (nonatomic,strong)NSString * height;

@property (nonatomic,strong)NSString * width;


#pragma mark - 单例

+(InforCenter *)sharedInforCenter;


@end



InforCenter.m文件

#import "InforCenter.h"

static InforCenter * inforcenter = nil;

@implementation InforCenter

#pragma mark - 单例

//通过单例这个类创建出来的对象指向同一个内存空间

+(InforCenter *)sharedInforCenter

{

    if (!inforcenter) {

        inforcenter = [[InforCenter alloc]init];

    }

    return inforcenter;

}


@end




ViewController.m文件

#import "ViewController.h"

#import "InforCenter.h"

#import "ShowViewController.h"


@interface ViewController ()

{

    UITextField * _nameTF;

    UITextField * _widthTF;

    UITextField * _heightTF;


}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor whiteColor];

    

    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(100, 500, 120, 30)];

    button.backgroundColor = [UIColor blackColor];

    [button setTitle:@"传值" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(pressedButton) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

    _nameTF = [[UITextField alloc]initWithFrame:CGRectMake(70, 50, 120, 30)];

    _widthTF = [[UITextField alloc] initWithFrame:CGRectMake(70, 100, 120, 30)];

    _heightTF = [[UITextField alloc] initWithFrame:CGRectMake(70, 150, 120, 30)];

    

    _nameTF.placeholder = @"名字";

    _widthTF.placeholder = @"宽度";

    _heightTF.placeholder = @"高度";

    

    _nameTF.layer.borderColor = [[UIColor blackColor] CGColor];

    _widthTF.layer.borderColor = [[UIColor blackColor] CGColor];

    _heightTF.layer.borderColor = [[UIColor blackColor] CGColor];

    _nameTF.layer.borderWidth = 1;

    _widthTF.layer.borderWidth = 1;

    _heightTF.layer.borderWidth = 1;

    

    [self.view addSubview:_nameTF];

    [self.view addSubview:_widthTF];

    [self.view addSubview:_heightTF];

}



- (void)pressedButton

{

    InforCenter * someInfor = [InforCenter sharedInforCenter];

    someInfor.userName = _nameTF.text;

    someInfor.width = _widthTF.text;

    someInfor.height = _heightTF.text;

    

    ShowViewController * showVC = [[ShowViewController alloc] init];

    [self presentViewController:showVC animated:YES completion:nil];

    

}


#pragma mark - 键盘收起

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self.view endEditing:YES];//设置点击空白地方键盘收起

}

@end






ShowViewController.m文件

#import "ShowViewController.h"

#import "InforCenter.h"

@interface ShowViewController ()


@end


@implementation ShowViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view .backgroundColor = [UIColor purpleColor];

    self.view.alpha = 0.9;

    

    UILabel * nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 50, 120, 30)];

    UILabel * widthLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 100, 120, 30)];

    UILabel * heightLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 150, 120, 30)];

    nameLabel.textColor = [UIColor blackColor];

    widthLabel.textColor = [UIColor blackColor];

    heightLabel.textColor = [UIColor blackColor];

    

    InforCenter * infor = [InforCenter sharedInforCenter];

    

    nameLabel.text = infor.userName;

    widthLabel.text = infor.width;

    heightLabel.text = infor.height;

    

    

    [self.view addSubview:nameLabel];

    [self.view addSubview:widthLabel];

    [self.view addSubview:heightLabel];

    

    

    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(100, 500, 120, 30)];

    button.backgroundColor = [UIColor cyanColor];

    [button setTitle:@"返回" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(pressedButton) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

}


- (void)pressedButton

{

    

    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];


}



@end




在ShowViewController类和ViewController类中引用了InforCenter里的属性值,单例的属性值是一个唯一存在的,调用获得的值也就是同一个。即可以用来传值。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值