TRFiristViewController
#import "TRFiristViewController.h"
#import "TRSecondViewController.h"
@interface TRFiristViewController ()<TRSecondViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@end
@implementation TRFiristViewController
- (IBAction)gotoSecondVC:(id)sender {
TRSecondViewController *vc2=[[TRSecondViewController alloc]init];
vc2.delegate=self;
[self presentViewController:vc2 animated:YES completion:nil];
}
-(void)secondViewController:(TRSecondViewController *)secondVC message:(NSString *)message{
self.showLabel.text=message;
}
#import <UIKit/UIKit.h>
//协议中使用了TRSecondController类,这里需要使用@class关键字提前告知有TRSecondController类存在,否则会报不存在TRSecondController类的错误
#import "TRFiristViewController.h"
@class TRSecondViewController;
//1. 定义一个委托协议,命名为TRSecondViewDelegate
@protocol TRSecondViewDelegate <NSObject>
- (void)secondViewController:(TRSecondViewController *)secondVC message:(NSString *)message;
@end
@interface TRSecondViewController : UIViewController
//2. 定义delegate属性
@property (nonatomic, weak)id<TRSecondViewDelegate> delegate;
@end
@interface TRSecondViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
@implementation TRSecondViewController
- (IBAction)backToFiristVc:(id)sender {
// self.firstVC.content=self.textField.text;
[self.delegate secondViewController:self message:self.textField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}