界面二的.m文件
// 制定協(xié)議
import@protocol WTwoViewControllerDelegate
// 聲明協(xié)議方法
-(void)changeValue:(NSString *)value;
?@end?
@interface WTwoViewController : UIViewController?
// 聲明一個(gè)Block類型的屬性
@property (nonatomic, unsafe_unretained) iddelegate;?
// 聲明一個(gè)UITextField類型的全局變量*txtValue
@property (nonatomic, strong) IBOutlet UITextField *txtValue;?
// 聲明Button的點(diǎn)擊事件方法
- (IBAction)pressChange:(id)sender;?
@end
// 界面二的.h文件
?- (IBAction)pressChange:(id)sender 方法中把代理派發(fā)出去,順便把窗口給銷毀,
代碼如下:- (IBAction)pressChange:(id)sender {???
// 代理傳值
? [self.delegate changeValue:self.txtValue.text];??
? [self dismissViewControllerAnimated:YES completion:nil];?
}?
界面二中的設(shè)置已經(jīng)完成,接下要在界面一中調(diào)用界面二的頭文件,并實(shí)現(xiàn)界面二協(xié)議中所制定的方法。
首先在WViewController.h中實(shí)現(xiàn)代理,
代碼如下:
#import "WTwoViewController.h" // 包含頭文件
// 聲明方法
@interface WViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *lblValue;
- (IBAction)pressCasting:(id)sender;
@end
其在WViewController.m的?
- (IBAction)pressCasting:(id)sender 方法中調(diào)用WTwoViewController,
并設(shè)置代理的回調(diào)方法,代碼如下:
- (IBAction)pressCasting:(id)sender {
WTwoViewController *controller = [[WTwoViewController alloc]initWithNibName:@"WTwoViewController" bundle:nil];
controller.delegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
- (void)changeValue:(NSString *)value{
// 改變UILabel的值
self.lblValue.text = value;
}