如果你還在苦惱block的傳值和回調,不妨看看這個Demo,自己整理的,希望對大家有幫助,這是下載地址 https://github.com/ShaoWenLe/BlockTestByValueAndCall-back.git
用的是storyboard結合Xib,如果看着不習慣,可以從上面鏈接下載源碼
//第一界面
// ViewController.h
// BlockTestByValueAndCall-back
//
// Created by 出神入化 on 15/8/15.
// Copyright (c) 2015年 出神入化. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
//
// ViewController.m
// BlockTestByValueAndCall-back
//
// Created by 出神入化 on 15/8/15.
// Copyright (c) 2015年 出神入化. All rights reserved.
//
#import "ViewController.h"
#import "TwoViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_label.text = @"隨便";
}
- (IBAction)next:(id)sender {
//用blockSelf修飾blockSelf.label 避免block塊內部_label被循環引用
__weak ViewController *blockSelf = self;
TwoViewController *twoVC = [[TwoViewController alloc] init];
//block返回值(跟代理寫法挺類似的,就是語法不同,代理的話是此處是self.delegate=self;)
[twoVC getValue:^(NSString *stringValue) {
NSLog(@"打印block傳的數值:%@",stringValue);
blockSelf.label.text = stringValue;
}];
[self.navigationController pushViewController:twoVC animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//第二界面
//
// TwoViewController.h
// BlockTestByValueAndCall-back
//
// Created by 出神入化 on 15/8/15.
// Copyright (c) 2015年 出神入化. All rights reserved.
//
#import <UIKit/UIKit.h>
//重新定義block類名 void返回值類型 BlockValue類名(重命名類名) NSString *stringValue參數
typedef void(^BlockValue)(NSString *stringValue);
@interface TwoViewController : UIViewController
//block屬性 此處要用copy修飾
@property (nonatomic, copy) BlockValue blockValue;
@property (weak, nonatomic) IBOutlet UITextField *textField;
- (void)getValue:(BlockValue)aBlock;
@end
//
// TwoViewController.m
// BlockTestByValueAndCall-back
//
// Created by 出神入化 on 15/8/15.
// Copyright (c) 2015年 出神入化. All rights reserved.
//
#import "TwoViewController.h"
#import "BlockDataHandle.h"
@interface TwoViewController ()
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)getValue:(BlockValue)aBlock
{
self.blockValue = aBlock;
}
//回調button
- (IBAction)callback:(id)sender {
BlockDataHandle *blockdataHandle = [[BlockDataHandle alloc] init];
//回調blockdataHandle,傳進去@"123",出來的string
[blockdataHandle getData:@"123" block:^(NSString *string) {
NSLog(@"打印回調之后的數據%@",string);
}];
}
//返回按鈕
- (IBAction)back:(id)sender {
//判斷是否執行setBlock方法,然后再執行里面的操作
if (self.blockValue) {
self.blockValue(_textField.text);
}
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//回調數據處理類
//
// blockData.h
// BlockTestByValueAndCall-back
//
// Created by 出神入化 on 15/8/15.
// Copyright (c) 2015年 出神入化. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef void(^BlockData)(NSString *string);
@interface BlockDataHandle : NSObject
@property (nonatomic, copy) BlockData blockData;
//set方法
-(void)setBlockData:(BlockData)blockData;
//傳入參數NSString
- (void)getData:(NSString *)string block:(BlockData)block;
@end
//
// blockData.m
// BlockTestByValueAndCall-back
//
// Created by 出神入化 on 15/8/15.
// Copyright (c) 2015年 出神入化. All rights reserved.
//
#import "BlockDataHandle.h"
@implementation BlockDataHandle
-(void)setBlockData:(BlockData)blockData
{
_blockData = blockData;
}
- (void)getData:(NSString *)string block:(BlockData)block
{
NSString *str = [NSString stringWithFormat:@"ASDFGH%@", string];
//調用block
block(str);
}
@end
