1、定义协议
UIViewPassValueDelegate.h
1 #import2 3 @protocol UIViewPassValueDelegate 4 -(void)passValue:(NSString *)value1;5 @end
2、A视图定义
1 #import "UIViewPassValueDelegate.h" 2 3 @interface AViewController : UIViewController4 5 -(IBAction)buttonClick:(id)sender; 6 7 -(void)passValue:(NSString *)value1{ 8 NSLog(@"A get value is %@",value1); 9 }10 11 -(IBAction)buttonClick:(id)sender{12 BViewController *b = [[BViewController alloc] init];13 b.delegate = self; // 将A作为一个变量传给B,这样在B中对delegate的任何操作其实就是对A的操作。14 [self presentViewController:b animated:YES completion:nil];15 }
3、B视图定义
1 #import "UIViewPassValueDelegate.h" 2 3 NSObject*delegate; 4 @property (nonatomic,retain) NSObject *delegate; 5 - (IBAction)buttonClick:(id)sender; 6 7 @synthesize delegate; 8 -(IBAction)buttonClick:(id)sender{ 9 [delegate passValue:@"ddd"];10 NSLog(@"B text = ddd");11 [self dismissViewControllerAnimated:YES completion:nil];12 }
这样我点击B视图的按钮时就可以将值传给A视图了。