UI23Block和单例传值

效果

161836_8C4q_3228897.png161849_AUci_3228897.png161904_2e7H_3228897.png

首先创建一个单例文件

--------------------------MyData.h-------------------------------

#import <Foundation/Foundation.h>

@interface MyData : NSObject

@property (nonatomic, strong)NSString *name;

+(MyData *) instance;

@end

 

-----------------------MyData.m-------------------------

#import "MyData.h"

@implementation MyData

static MyData *myData = nil;

+(MyData *) instance {

    if(myData==nil){

        myData = [[MyData alloc] init];

    }

    return myData;

}

@end

-----------------设置导航栏左侧按钮(跳转)---------------------------

RootViewController.m 

//引用 MyData.h

#import "MyData.h"

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor orangeColor];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"跳转" style:UIBarButtonItemStylePlain target:self action:@selector(jumpPage)];

    //创建个单例对象并赋值

    MyData *data = [MyData instance];

    data.name = @"小明";    

}

// 赋值并跳转到第二个页面

-(void) jumpPage {

    SecondViewController *second = [[SecondViewController alloc] init];    

    //Block传值

    second.chuanZhiBlock = ^(NSString *str){

       // NSLog(@"%@",str);

    };

     [self.navigationController pushViewController:second animated:YES]; 

}

-------------------第二个页面处理-----------------------------------

//  SecondViewController.h

//  UI23Block和单例传值

//  Created by focus on 2017/1/23.

//  Copyright © 2017年 focus. All rights reserved.

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

//声明Block

@property(nonatomic, copy)void(^chuanZhiBlock)(NSString * Str);

@end

============SecondViewController.m================================

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor yellowColor];    

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(jumpBack)];

    MyData *data = [MyData instance];

    NSLog(@"获取单例中的Name值:%@", data.name);

}

-(void) jumpBack {

    [self.navigationController popViewControllerAnimated:YES];

    self.chuanZhiBlock(@"Block传值过来。");

}

 

 

 

转载于:https://my.oschina.net/VincentOSC/blog/830307

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值