简单的block传值(逆向)

96
探路者1202
2017.05.25 14:56* 字数 229

第一页面 

.h 文件中

@interfaceViewController :UIViewController

@property(weak,nonatomic)IBOutletUILabel*nextPassedValue;

- (IBAction)next:(UIButton*)sender;

@end

.m文件中

@interfaceViewController()

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.navigationItem.title=@"一";

}

//pushnext

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender

{

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

SecondViewController*tfVC = segue.destinationViewController;

//赋值Block,并将捕获的值赋值给UILabel

tfVC.returnValueBlock= ^(NSString*passedValue)

{

self.nextPassedValue.text= passedValue;

};

}

- (IBAction)next:(UIButton*)sender

{

[self performSegueWithIdentifier:@"pushnext"sender:self];

}

第二个页面

.h

//自定义类型

typedefvoid(^ReturnValueBlock) (NSString*strValue);

@interfaceSecondViewController :UIViewController


//声明一个ReturnValueBlock属性,这个Block是获取传值的界面传进来的

@property(nonatomic,copy)ReturnValueBlockreturnValueBlock;

@end


.m

@interfaceSecondViewController()

- (IBAction)back:(UIButton*)sender;

@property(weak,nonatomic)IBOutletUITextField*inputText;

@end

@implementationSecondViewController

- (void)viewDidLoad

{

[superviewDidLoad];

self.navigationItem.title=@"二";

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (IBAction)back:(UIButton*)sender

{

NSString*inputString =self.inputText.text;

if(self.returnValueBlock) {

//将自己的值传出去,完成传值

self.returnValueBlock(inputString);

}

[self.navigationControllerpopViewControllerAnimated:YES];

}

日记本
Web note ad 1