IOS(UI)_委托传值

5 篇文章 0 订阅

ViewController.m


#import "ViewController.h"
#import "NextViewController.h"

@interface ViewController ()<NextProtocol>
{
    UILabel *_label;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.title = @"ViewController";

    _label = [UILabel new];
    _label.text = @"LOL";
    _label.backgroundColor = [UIColor yellowColor];
    _label.textAlignment = NSTextAlignmentCenter;

    _label.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:_label];

    NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:100];
    [self.view addConstraint:constraint1];

    NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraint:constraint2];

    NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:200];
    [self.view addConstraint:constraint3];

    NSLayoutConstraint *constraint4 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30];
    [self.view addConstraint:constraint4];

    UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [pushButton setTitle:@"push" forState:UIControlStateNormal];
    [pushButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];

    pushButton.translatesAutoresizingMaskIntoConstraints = NO;
    [pushButton addTarget:self action:@selector(pushBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pushButton];

    NSLayoutConstraint *constraint11 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_label attribute:NSLayoutAttributeBottom multiplier:1 constant:50];
    [self.view addConstraint:constraint11];

    NSLayoutConstraint *constraint12 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraint:constraint12];

    NSLayoutConstraint *constraint13 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:100];
    [self.view addConstraint:constraint13];

    NSLayoutConstraint *constraint14 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30];
    [self.view addConstraint:constraint14];


}

#pragma mark ---------------------
-(void)pushBtnAction:(UIButton *)sender
{
    NextViewController *nextViewController = [[NextViewController alloc]init];

   // nextViewController.text = _label.text;
    [self.navigationController pushViewController:nextViewController animated:YES];

    //=====让ViewController遵循协议
    nextViewController.delegate = self;
}
//调用协议里面的方法
-(void)transferString:(NSString *)string
{
    _label.text = string;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


/*
 从A页面推到B页面,然后B页面要回传值给A页面。
 若使用委托传值的方式。那么首先需要在定义一个协议,然后在B页面定义一个循序该协议的代理的属性
 判断代理是否存在并且代理是否循序协议。若存在,再调用协议里面的方法。

 在A页面,让A页面遵循该协议,让后让B页面的对象的代理 = self
 最后再实现一些协议里面的方法,并以此获得回传的参数

 */

NextViewController.h

#import <UIKit/UIKit.h>

//委托模式在UI中属于回调传值=============
@protocol NextProtocol <NSObject>
//在委托里面定义一个方法=============
-(void)transferString:(NSString *)string;

@end

@interface NextViewController : UIViewController

@property(nonatomic,strong)NSString *text;
//定义一个协议 =======
@property (nonatomic,assign)id<NextProtocol>delegate;

@end

NextViewController.m


#import "NextViewController.h"
//调用TextField API里面的协议 用来取消第一响应者
@interface NextViewController ()<UITextFieldDelegate>

@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"NextViewController";

    UILabel *label = [UILabel new];
    label.backgroundColor = [UIColor yellowColor];

    label.text = self.text;

    label.textAlignment = NSTextAlignmentCenter;

    label.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:label];

    NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:100];
    [self.view addConstraint:constraint1];

    NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraint:constraint2];

    NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:200];
    [self.view addConstraint:constraint3];

    NSLayoutConstraint *constraint4 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30];
    [self.view addConstraint:constraint4];


    UITextField *textField = [[UITextField alloc]init];
    textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    textField.layer.borderWidth = 1.0;
    textField.layer.cornerRadius = 5.0;
    textField.layer.masksToBounds = YES;
    //让谁遵循委托==============
    textField.delegate = self;

    textField.placeholder = @"请输入要传的值..";
    textField.tag = 2000;
    textField.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:textField];

    NSLayoutConstraint *constraint11 = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraint:constraint11];

    NSLayoutConstraint *constraint12 = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:300];
    [self.view addConstraint:constraint12];

    NSLayoutConstraint *constraint13 = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:200];
    [self.view addConstraint:constraint13];

    NSLayoutConstraint *constraint14 = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30];
    [self.view addConstraint:constraint14];



    UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [pushButton setTitle:@"back" forState:UIControlStateNormal];
    [pushButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
//为按钮添加点击事件
    [pushButton addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    pushButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:pushButton];

    NSLayoutConstraint *constraint21 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraint:constraint21];

    NSLayoutConstraint *constraint22 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:360];
    [self.view addConstraint:constraint22];

    NSLayoutConstraint *constraint23 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:60];
    [self.view addConstraint:constraint23];

    NSLayoutConstraint *constraint24 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30];
    [self.view addConstraint:constraint24];


}

#pragma mark -----------------
-(void)backBtnAction:(UIButton *)sender
{

    UITextField *textField = (UITextField *)[self.view viewWithTag:2000];

    //判断textFiel里面的内容是否为空
    if (textField.text.length == 0)
    {
        NSLog(@"请输入回传值,亲");

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入回传值,亲" preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alertController addAction:action];

//        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            
//        }];
//        [alertController addAction:action1];
//        
//        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            
//        }];
//        [alertController addAction:action2];

        [self presentViewController:alertController animated:YES completion:^{

        }];

    }
    else
    {
        //判断是否遵循协议=====================
        if (self.delegate && [self.delegate conformsToProtocol:@protocol(NextProtocol)])
        {
            //遵循协议之后 调用协议里面的方法把text的值传过去
            [self.delegate transferString:textField.text];
        }
        //返回跳转的UIViewController
        [self.navigationController popViewControllerAnimated:YES];
    }

}

#pragma mark ---------UITextFieldDelegate(当按键盘return键是取消第一响应者)---------
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //
    [textField resignFirstResponder];
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值