您的位置:首页 > 移动开发 > IOS开发

IOS学习之——界面2传值到界面1(代理方法)

2016-04-15 14:33 357 查看




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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: