iOS开发 UI高级之代理实现传值

基本思路:创建一个实现传值协议,两个控制器都需实现协议,利用代理进行两端传值;

根视图:

#import "RootViewController.h"
#import "ModalViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1) 设置背景颜色
    self.view.backgroundColor = [UIColor cyanColor];
    
    //2) 设置一个Label
    //a) 创建一个Label
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 100, 270, 30)];
    //b) 设置该label的tag
    label.tag = 2000;
    //c) 设置label的内容
    label.text = @"代理模式的传值";
    //d) 设置背景颜色
    label.backgroundColor = [UIColor orangeColor];
    //e) 设置字体颜色
    label.textColor = [UIColor whiteColor];
    //f) 设置居中方式
    label.textAlignment = NSTextAlignmentCenter;
    //g) 添加label
    [self.view addSubview:label];
    
    //3) 设置跳转你的button
    //a) 创建button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    //b) 设置其frame
    button.frame = CGRectMake(0, 0, 200, 30);
    //c) 设置其在屏幕的中心
    button.center = self.view.center;
    //d) 设置背景颜色
    button.backgroundColor = [UIColor lightGrayColor];
    //e) 设置显示的内容
    [button setTitle:@"跳转" forState:UIControlStateNormal];
    //f) 设置相应事件
    [button addTarget:self
               action:@selector(buttonAction:)
     forControlEvents:UIControlEventTouchUpInside];
    //g) 添加到页面上
    [self.view addSubview:button];
    
}

- (void) buttonAction: (UIButton *) button {
    
    //1) 创建一个模态视图
    ModalViewController *modalViewController = [[ModalViewController alloc] init];
    //2. 设置弹窗模式
    modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    //3. 设置代理对象
    modalViewController.delegate = self;
    //4. 模态视图
    [self presentViewController:modalViewController animated:YES completion:nil];
}

#pragma mark - 代理方法:值传递
- (void) changeTextLabelValue: (NSString *) text {
    //1) 获取Label
    UILabel *label = (UILabel *)[self.view viewWithTag:2000];
    //2) 传值
    label.text = text;
}
下个控制器:


<span style="font-size:18px;">#import "ModalViewController.h"

@interface ModalViewController ()

@end

@implementation ModalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 35)];
    textFiled.center = self.view.center;
    textFiled.backgroundColor = [UIColor cyanColor];
    textFiled.tag = 1000;
    textFiled.placeholder = @"请输入文字...";
    [self.view addSubview:textFiled];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 300, 50, 30);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor orangeColor];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

-(void)buttonAction:(UIButton *)button{
    
    UITextField *str = (UITextField *)[self.view viewWithTag:1000];
    
    [self.delegate changeValuesForVC:str.text];
    
    [self dismissViewControllerAnimated:YES completion:nil];
        
}
</span>

代理协议:

<span style="font-size:18px;">#import <Foundation/Foundation.h>

@protocol ChageValues <NSObject>
// 代理方法
-(void)changeValuesForVC:(NSString *)text;
@end
</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值