iOS多界面传值之--代理传值

本文介绍iOS开发中如何使用代理模式实现从SecondViewController回传数据到ViewController的方法。通过定义协议、设置代理并实现代理方法,完成逆向传值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代理传值

代理传值一般用于逆向传值,即第二个页面传值给第一个页面
ViewController页面push到SecondViewController页面,如果SecondViewController页面的信息想回传(回调)到ViewController页面,用代理传值,其中SecondViewController定义协议和声明代理,ViewController确认并实现代理,ViewController作为SecondViewController的代理

  1. 第一步
  2. 首先新建一个SecondViewController,并且在SecondViewController里面
  3. 定义协议
  4. 设置协议中的方法
  5. 声明代理
  6. 第二步
  7. 在SecondViewController.m中为其绑定相应方法
  8. 第三步
  9. ViewController.m中
  10. 设置代理
  11. 服从协议
  12. 实现代理方法

下面我们上代码

1.在SecondViewController.h中创建协议方法

import <UIKit/UIKit.h>

//首先在SecondViewController.h中创建协议方法
@class SecondViewController;
//定义协议
@protocol PushValueDelegate <NSObject>

//设置协议中的方法
-(void)viewController:(SecondViewController *)ViewController didPushValueWithInfo:(id)info;


@end


@interface SecondViewController : UIViewController

//声明代理
@property (nonatomic,strong)id<PushValueDelegate>delagate;

@end

2.在SecondViewController.m文件中

#import "SecondViewController.h"

@interface SecondViewController ()

@property (nonatomic,strong)UITextField *textField;
@property (nonatomic,strong)NSString *textString;

@end

@implementation SecondViewController

-(void)viewWillDisappear:(BOOL)animated{

//    设置视图将要消失时.通过代理传值
//    判断代理是否存在,并且设置代理能够响应代理方法时,才执行代理方法
    if (self.delagate && [self.delagate respondsToSelector:@selector(viewController:didPushValueWithInfo:)]) {

        [self.delagate viewController:self didPushValueWithInfo:_textField.text];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor cyanColor];
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textField];


}

3.回到我们的ViewController.m中,首先我们需要导入一下头文件,然后在遵循在SecondViewController里面声明的协议,最后在viewDidLoad里面创建UITextField 和 UIButton


#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()<PushValueDelegate>

@property (nonatomic,strong) UITextField *textField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"代理传值";
    self.view.backgroundColor = [UIColor yellowColor];

    /**
      代理传值(场景)一般用于逆向传值,即第二个界面传值给第一个界面
      ViewController页面push到SecondViewController页面,如果SecondViewController页面的信息想回传(回调)到ViewController页面,用代理传值,其中SecondViewController定义协议和声明代理,ViewController确认并实现代理,ViewController作为SecondViewController的代理

     第一步:首先,在SecondViewController.h中

     (1).定义协议

     (2).设置协议中的方法

     (3).声明代理

     然后,在SecondViewController.m中

     (1).为其绑定相应方法


     第二步:在FirstViewController.m中

     (1).设置代理

     (2).服从协议
     */


    _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.borderStyle = UITextBorderStyleRoundedRect;

    [self.view addSubview:_textField];


    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(150, 200, 80, 44);

    [btn setTitle:@"Next" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];


}

4.实现一下Button里面的点击事件方法,并设置代理

-(void)nextAction:(UIButton *)sender{

    SecondViewController *secondeVC = [[SecondViewController alloc]init];
//    设置代理
    secondeVC.delagate = self;

    [self.navigationController pushViewController:secondeVC animated:YES];


}

5.实现代理方法

//实现代理方法
-(void)viewController:(SecondViewController *)ViewController didPushValueWithInfo:(id)info{

    _textField.text = info;
}

6.由于是通过navigationController跳转到SecondViewController里面的,所以我们需要在AppDelegate里面创建一个UINavigationController

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
    [self.window makeKeyAndVisible];


    return YES;
}

7.模拟器运行效果如下
这里写图片描述

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值