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



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];
}















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值