iOS block传值

//
//  FirstViewController.m
//  TransformValue1
//
//  Created by 王聪 on 14/8/7.
//  Copyright (c) 2014   Congwang. All rights reserved.
//

#import  "FirstViewController.h"
#import
  "SecondViewController.h"

@interface  FirstViewController  ()


@end

@implementation  FirstViewController

- (
void )viewDidLoad {
    [
super  viewDidLoad ];
   
  // Do any additional setup after loading the view from its nib.
}
// 生成按钮的点击事件
- (
IBAction )FirstButtonAction:( UIButton  *)sender {
   
  // 创建对象
   
  SecondViewController  *secondVC = [[ SecondViewController alloc ]  init ];
     // 给属性赋值
    secondVC.secondStr  =  @"Wangcong " ;
     // 防止循环引用
   
  __block  typeof  ( self ) mySelf =  self ;
   
  // block 属性赋值
    [secondVC
  setMyBlock :^( NSString  *str) {
    
  //  把一些回调操作写入 block, block 调用的时候会触发
        mySelf.
FirstLabel . text  = str;
    }];
   
    [secondVC
  setTestBlock :^( NSString  *test) {
       
  NSLog ( @"%@" , test);
    }];
   
   
   
  // 推出第二个控制器
    [
self . navigationController  pushViewController :secondVC animated : YES ];
    [secondVC
  release ];
}

- (
void )didReceiveMemoryWarning {
    [
super  didReceiveMemoryWarning ];
   
  // Dispose of any resources that can be recreated.
}



- (
void )dealloc {
    [
_FirstLabel  release ];
    [
_FirstLabel  release ];
    [
super  dealloc ];
}
@end


//
//  SecondViewController.h
//  TransformValue1
//
//  Created by lanouhn on 14/8/7.
//  Copyright (c) 2014   Congwang. All rights reserved.
//

#import 
//重新定义一种myBlock类型
typedef void (^Block)(NSString *);
@interface SecondViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *SecondLabel;
//字符串属性
@property (nonatomic, copy)NSString *secondStr;
//声明block属性1
@property (nonatomic, copy)void(^myBlock)(NSString *);
//定义block属性的第二种方法, 结合类型重定义
@property (nonatomic, copy)Block myBlock2;
//block调用要保证在block赋值之后执行
//测试
@property (nonatomic, copy)void(^testBlock)(NSString *);


@end

//
//  SecondViewController.m
//  TransformValue1
//
//  Created by lanouhn on 14/8/7.
//  Copyright (c) 2014 Congwang. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
   
 // Do any additional setup after loading the view from its nib.
   
 //label赋值
   
 self.SecondLabel.text = self.secondStr;
}
- (
IBAction)SecononAction:(UIButton *)sender {
  //点击按钮触发block
    self.myBlock (@"大佐");
    self.testBlock(@"测试专属");
   
 //返回上一层
    [
self.navigationController popViewControllerAnimated:YES];
}

- (
void)didReceiveMemoryWarning {
    [
super didReceiveMemoryWarning];
   
 // Dispose of any resources that can be recreated.
}


- (void)dealloc {
    [
_SecondLabel release];
   
 Block_release(self.myBlock);
   
 Block_release(self.myBlock2);
    [
super dealloc];
}
@end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值