iOS跨界面传值

本文详细介绍iOS应用中两个界面间的数据传递方法,包括正向传递和反向传递(协议法),并提供具体代码实现,帮助开发者理解并掌握界面间数据交互的核心技巧。

第一种:正传,即将第一个界面上的值传递给第二个界面

思路:
  • 第二个界面的ViewController里定义一个属性NSString *str
  • 在第一个界面跳转第二个界面的button点击事件中将第二个界面实例化,并将要传递的信息赋值给str
  • 第二个界面中接收str赋值给需要的对象
代码:

ViewController.m 里:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _textField1 = [[UITextField alloc] initWithFrame:CGRectMake(50, 300, 200, 50)];
    [self.view addSubview:_textField1];
    _textField1.layer.masksToBounds = YES;
    _textField1.layer.borderWidth = 2;
    _textField1.layer.cornerRadius = 5;
    _textField1.layer.borderColor = [UIColor blackColor].CGColor;
    
    _button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview:_button];
    _button.frame = CGRectMake(100, 370, 100, 50);
    _button.backgroundColor = [UIColor orangeColor];
    [_button setTitle:@"up" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchDown];
    
}
//button点击事件
- (void)press{
    TwoViewController *two = [[TwoViewController alloc] init];
    two.str = _textField1.text;
    [self presentViewController:two animated:NO completion:nil];
}

TwoViewController.m 里:

 _textField2 = [[UITextField alloc] initWithFrame:CGRectMake(50, 300, 200, 50)];
    [self.view addSubview:_textField2];
    _textField2.text = _str;

第二种:反传,即将第二个界面上的值传递给第一个界面(协议法)

思路:
  • 在第二个界面.h 文件中声明协议
  • 在第一个界面.h 文件中代理协议
  • 在第一个界面.m 文件中使用协议方法,在跳转界面函数中声明代理
  • 在第二个界面中使用协议委托传值

第一个界面称为代理对象 ,第二个界面称为委托对象

代码:

在第二个界面.h 文件中声明协议

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
//协议定义
@protocol TwoDelegate <NSObject>
//协议方法
- (void)pass:(NSString *)str;

@end

@interface TwoViewController : UIViewController

@property UITextField *textField2;
@property NSString *str;
@property UIButton *button;
//协议代理
@property id <TwoDelegate> twoDelegate;

@end

在第一个界面.h 中代理协议

#import <UIKit/UIKit.h>
#import "TwoViewController.h"

@interface ViewController : UIViewController
<
TwoDelegate
>

@property UITextField *textField1;
@property UIButton *button;

@end

在第一个界面.m 文件中使用协议方法,在跳转界面函数中声明代理(此处我点击button事件跳转界面)

#import "ViewController.h"
#import "TwoViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _textField1 = [[UITextField alloc] initWithFrame:CGRectMake(50, 300, 200, 50)];
    [self.view addSubview:_textField1];
    _textField1.layer.masksToBounds = YES;
    _textField1.layer.borderWidth = 2;
    _textField1.layer.cornerRadius = 5;
    _textField1.layer.borderColor = [UIColor blackColor].CGColor;
    
    _button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview:_button];
    _button.frame = CGRectMake(100, 370, 100, 50);
    _button.backgroundColor = [UIColor orangeColor];
    [_button setTitle:@"up" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchDown];
    
}
//button点击事件
- (void)press{
    TwoViewController *two = [[TwoViewController alloc] init];
    //设置第二个窗口中的delegate为第一个窗口的self
    two.twoDelegate = self;
    [self presentViewController:two animated:NO completion:nil];
}

//执行代理方法
- (void)pass:(NSString *)str {
    _textField1.text = str;
}

在第二个界面中使用协议委托传值

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];
    
    _textField2 = [[UITextField alloc] initWithFrame:CGRectMake(50, 300, 200, 50)];
    [self.view addSubview:_textField2];
    _textField2.text = _str;
    _textField2.layer.masksToBounds = YES;
    _textField2.layer.borderWidth = 2;
    _textField2.layer.cornerRadius = 5;
    _textField2.layer.borderColor = [UIColor blackColor].CGColor;
    
    _button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview:_button];
    _button.frame = CGRectMake(100, 370, 100, 50);
    _button.backgroundColor = [UIColor orangeColor];
    [_button setTitle:@"back" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchDown];
    
}

- (void)press{
    [self dismissViewControllerAnimated:NO completion:nil];
    //代理执行协议
    if([_twoDelegate respondsToSelector:@selector(pass:)]){
        [_twoDelegate pass:_textField2.text];
    }
}
第二种方法的理解可以参考UITableView中协议上使用,唯一的区别在于协议是自己定义的

效果图可能不大看的出来是传值过去的,但还是放上吧

刚开始时界面一:

点击up进入界面二并输入值:
在这里插入图片描述
点击back回到界面一:
在这里插入图片描述
值就传给界面一啦!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值