如果两个试图只是简单的1对1传值的话可以用Block代替delegate,使用简单。
在Viewcontroller push/present BlockViewController 之后,可向Viewcontroller传回BlockViewController的某个Value ,
有两种方式:
1.可在present/push之后直接传回
2.可在push之后,Viewcontroller中某个按钮触发时传回
如以下demo
Viewcontroller.m
BlockViewController.h
#import <UIKit/UIKit.h>
typedef void (^myVCBlock) (NSString * str);
@interface BlockViewController : UIViewController
-(void)returnName:(myVCBlock)block;
@property(nonatomic,assign)myVCBlock block;
@end
BlockViewController.m
在Viewcontroller push/present BlockViewController 之后,可向Viewcontroller传回BlockViewController的某个Value ,
有两种方式:
1.可在present/push之后直接传回
2.可在push之后,Viewcontroller中某个按钮触发时传回
如以下demo
Viewcontroller.m
#import "ViewController.h"
#import "BlockViewController.h"
@interface ViewController (){
BlockViewController * VC;
UILabel * _testLabel;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//接收回传值
_testLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 40)];
_testLabel.text=@"label";
[self.view addSubview:_testLabel];
//点击后更改_testLabel.text
UIButton * touchBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[touchBtn setBackgroundColor:[UIColor orangeColor]];
[touchBtn setFrame:CGRectMake(
(self.view.frame.size.width-50)/2,
(self.view.frame.size.height-50)/2,
50,
50)];
[touchBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:touchBtn];
UIButton * pushBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[pushBtn setBackgroundColor:[UIColor orangeColor]];
[pushBtn setFrame:CGRectMake(
(self.view.frame.size.width-50)/2,
(self.view.frame.size.height-50)/2+100,
50,
50)];
[pushBtn setTitle:@"push" forState:UIControlStateNormal];
[pushBtn addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pushBtn];
}
-(void)btnClick{
/****************
可在点击某个按钮之后设置,_testLabel.text
***************/
[self setViewController];
}
-(void)push{
VC=[[BlockViewController alloc]init];
[self presentViewController:VC animated:YES completion:^{
/****************
可在present/push之后设置,_testLabel.text
***************/
//[self setViewController];
}];
}
//设置_testLabel.text
-(void)setViewController{
//1.调用方法回传
[VC returnName:^(NSString *str) {
_testLabel.text=[NSString stringWithFormat:@"%@",str];
}];
//2.直接回传
//_testLabel.text=VC.block;
}
@end
BlockViewController.h
#import <UIKit/UIKit.h>
typedef void (^myVCBlock) (NSString * str);
@interface BlockViewController : UIViewController
-(void)returnName:(myVCBlock)block;
@property(nonatomic,assign)myVCBlock block;
@end
BlockViewController.m
@synthesize block;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton * viewBackBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[viewBackBtn setBackgroundColor:[UIColor orangeColor]];
[viewBackBtn setFrame:CGRectMake(
(self.view.frame.size.width-50)/2,
(self.view.frame.size.height-50)/2,
50,
50)];
[viewBackBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:viewBackBtn];
}
//回传
-(void)returnName:(myVCBlock)block{
block([self setValue]);
}
//设置回传值
-(NSString *)setValue{
return @"你好,这是一个回传值";
}
-(void)btnClick{
//也可直接回传或者通过-(void)returnName:(myVCBlock)block方法回传
//block=(myVCBlock)([self setValue]);
[self dismissViewControllerAnimated:YES completion:^{
}];
}