iOS 多面传值之 -- 单例传值

单例传值

  1. 单例可以保证其某个类的实例在程序中是唯一的,便于进行资源和数据的共享
  2. 单例传值可以理解为定义一个全局静态变量进行传值,将第二个页面的内容传入第一个页面并显示。
  3. 单例可以正向、逆向传值.但是如果想要正向传值的话,可以直接简单粗暴地使用属性传值….下面我们来谈谈单例的逆向传值…

3.新建一个Person类

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic,strong) NSString *string;

+(instancetype)shareInstance;


@end
#import "Person.h"

@implementation Person

//声明一个static变量
static  Person *instrance = nil;

//使用类方法生成这个类唯一的实例
+(Person *)shareInstance{
    if (instrance == nil) {

        instrance = [[super alloc]init];
    }
    return instrance;

}
@end

4.然后在SecondViewController.m中导入头文件,并且声明一个UITextField 和 UIbutton的属性.创建一个UITextField 和 UIbutton

#import "SecondViewController.h"
#import "Person.h"

@interface SecondViewController ()

@property (nonatomic,strong) UITextField *textF;
@property (nonatomic,strong) UIButton *backBtn;


@end

@implementation SecondViewController
-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    _textF.text = [Person shareInstance].string;

}

-(UITextField *)textF{

    if (!_textF) {
        _textF = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
        _textF.borderStyle = UITextBorderStyleRoundedRect;
        _textF.backgroundColor = [UIColor whiteColor];


    }
    return _textF;
}

-(UIButton *)backBtn{

    if (!_backBtn ) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _backBtn.frame = CGRectMake(20, 200, self.view.bounds.size.width-40, 44);
        _backBtn.backgroundColor = [UIColor blueColor];
        _backBtn.tintColor = [UIColor whiteColor];
        _backBtn.titleLabel.font = [UIFont systemFontOfSize:25.0];
        [_backBtn setTitle:@"Back" forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];


    }
    return _backBtn;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.textF];
    [self.view addSubview:self.backBtn];


}


-(void)backAction:(UIButton *)sender{
    NSLog(@"%p",[Person shareInstance]);
//    返回时为单例属性赋值
    [Person shareInstance].string = _textF.text;
    [self.navigationController popViewControllerAnimated:YES];

5.回到ViewController.m中

#import "ViewController.h"
#import "Person.h"
#import "SecondViewController.h"

@interface ViewController ()
@property (nonatomic,strong)UITextField *textField;
@property (nonatomic,strong)UIButton *btn ;


@end

@implementation ViewController
-(UITextField *)textField{

    if (!_textField) {
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        _textField.backgroundColor = [UIColor whiteColor];


    }
    return _textField;
}
-(UIButton *)btn{

    if (!_btn) {

        _btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _btn.frame = CGRectMake(20, 200, self.view.bounds.size.width- 40, 40);
        _btn.backgroundColor = [UIColor blueColor];
        _btn.titleLabel.font = [UIFont systemFontOfSize:25.0];
        [_btn setTitle:@"Next" forState:UIControlStateNormal];
        [_btn setTintColor:[UIColor whiteColor]];
        [_btn addTarget:self action:@selector(NextAction:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _btn;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *  单例传值
     单例可以保证其某个类的实例在程序中是唯一的,便于进行资源和数据的共享
     单例传值可以理解为定义一个全局静态变量进行传值,将第二个页面的内容传入第一个页面并显示。

     */
    self.navigationItem.title = @"单例传值";
    self.view. backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.textField];
    [self.view addSubview:self.btn];


}

//接收由第二个界面pop回时textF里面的数据
-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    _textField.text = [Person shareInstance].string;


}


-(void)NextAction:(UIButton *)sender{

//    跳转到下个界面的同时为单例属性赋值
    _textField.text = [Person shareInstance].string;
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secondVC animated:YES];

}

6.由于我们是通过navigationController push到第二个页面的,所以,我们要在AppDelegate创建一个UINavigationController

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
    [self.window makeKeyAndVisible];


    return YES;
}

7.模拟器运行效果如下
这里写图片描述

这里写图片描述

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值