在第二個頁面.h文件中:
#import <UIKit/UIKit.h>
//1.聲明協(xié)議截珍,習(xí)慣命名方式為 類型+Delegate
//2.@pro不允許緊貼著#import寫,沒有代碼提示
//一.代理回傳第一步坡椒,聲明協(xié)議
@protocol NextViewControllerDelegate <NSObject>
-(void)showText:(NSString *)text;
@end
@interface NextViewController : UIViewController
//二.代理回傳第二步,聲明 協(xié)議類型的屬性
@property(nonatomic) id<NextViewControllerDelegate> delegate;
@end
在第二個頁面的.m中
#import "NextViewController.h"
@interface NextViewController ()
@end
@implementation NextViewController
- (IBAction)clickReturn:(UITextField *)sender {
//三.代理回傳第三步窥淆,在適當(dāng)?shù)奈恢谜{(diào)用協(xié)議方法
[_delegate showText:sender.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
在第一個頁面.m文件中:
#import "MyViewController.h"
#import "NextViewController.h"
//四.代理回傳第四步送挑,引入?yún)f(xié)議
@interface MyViewController ()<NextViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation MyViewController
- (IBAction)click:(id)sender {
NextViewController *nextVC = [NextViewController new];
//五.代理回傳第五步,設(shè)置當(dāng)前視圖控制器為 下一頁的 代理
nextVC.delegate = self;
[self presentViewController:nextVC animated:YES completion:nil];
}
//六.代理回傳第六步锦溪,引入?yún)f(xié)議方法
- (void)showText:(NSString *)text{
_label.text = text;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end