第一个控制器:
-(void)barAction:(UIBarButtonItem*)sender
{
NextViewController *next=[[NextViewController alloc]init];
//拿当前页面的值传到后一个页面
next.stringValue=self.rv.textField.text;//属性传值
//block传值
__weak RootViewController *weakSelf=self;//weakSelf可以在block中修改,__week改变相互持有的状态,避免释放的时候无法释放
//block前面传后面
//next.pv=^{
// return weakSelf.rv.textField.text;
//};
//block传值
next.mb=^(NSString *str){
weakSelf.rv.textField.text=str;
};
[self.navigationController pushViewController:next animated:YES];
}
第二个控制器:
.h文件
typedef void(^MyBlock)(NSString *str);//block传值,定义一个block块
//typedef NSString* (^PassValue)();//block前面传后面
@interface NextViewController : UIViewController
//接受前一个页面传过来的值
@property(nonatomic,strong)NSString *stringValue;//属性传值
//block传值
@property(nonatomic,copy)MyBlock mb;//block传值
//@property(nonatomic,copy)PassValue pv;//block前面传后面
.m文件
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIBarButtonItem *bar=[[UIBarButtonItem alloc]initWithTitle:@"退回" style: UIBarButtonItemStyleDone target:self action:@selector(barAction:)];
self.navigationItem.leftBarButtonItem=bar;
//用前一个页面传过来的值赋给当前页面
self.nv.textField.text=self.stringValue;//属性传值
//self.nv.textField.text=self.pv;//block前面传后面
}
-(void)barAction:(UIBarButtonItem*)sender
{
self.mb(self.nv.textField.text);//block传值
[self.navigationController popViewControllerAnimated:YES];
}