美文网首页
UI04_界面之间的通信-代理正反向传值(17-08-08)

UI04_界面之间的通信-代理正反向传值(17-08-08)

作者: Hilarylii | 来源:发表于2017-08-08 16:08 被阅读0次
//
//  ViewController.m
//  UI04_界面之间的通信
//
//  Created by lanou3g on 17/8/8.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

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

//遵守协议
@interface ViewController ()<SecondViewControllerDelegate>

@property (nonatomic, retain) UITextField *textField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    self.textField.placeholder = @"请输入内容";
    [self.view addSubview:self.textField];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 200, 100, 100);
    button.backgroundColor = [UIColor orangeColor];
    [button setTitle:@"下一页" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)buttonAction:(UIButton *)button {
    SecondViewController *secondViewController = [[SecondViewController alloc] init];
    //建立代理关系 (属性传值)
    secondViewController.delegate = self;
    secondViewController.text = self.textField.text;
    [self presentViewController:secondViewController animated:YES completion:nil];
}
//实现协议方法
- (void)gettextFieldValue:(NSString *)text {
    //传过来的text,给本页中的text
    self.textField.text = text;
}

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

@end

//
//  SecondViewController.h
//  UI04_界面之间的通信
//
//  Created by lanou3g on 17/8/8.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import <UIKit/UIKit.h>


//委托方制定协议,代理人必须遵守
//协议
@protocol SecondViewControllerDelegate <NSObject>

- (void)gettextFieldValue:(NSString *)text;

@end

@interface SecondViewController : UIViewController
//给label显示
@property (nonatomic, retain) NSString *text;

//代理的属性  //用来设置代理,并且遵守协议
@property (nonatomic, assign) id<SecondViewControllerDelegate>delegate;

@end

//
//  SecondViewController.m
//  UI04_界面之间的通信
//
//  Created by lanou3g on 17/8/8.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@property (nonatomic, retain) UITextField *textField2;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
    label.backgroundColor = [UIColor redColor];
    label.text = self.text;
    [self.view addSubview:label];
    
    self.textField2 = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];
    self.textField2.placeholder = @"请输入内容:";
    [self.view addSubview:self.textField2];
    
    UIButton *backButtton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButtton.frame = CGRectMake(100, 300, 100, 100);
    backButtton.backgroundColor = [UIColor greenColor];
    [backButtton setTitle:@"上一页" forState:UIControlStateNormal];
    [backButtton addTarget:self action:@selector(backButttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButtton];
    
}

- (void)backButttonAction:(UIButton *)buttton {
    //利用代理去回传数据
    [self.delegate gettextFieldValue:self.textField2.text];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

相关文章

网友评论

      本文标题:UI04_界面之间的通信-代理正反向传值(17-08-08)

      本文链接:https://www.haomeiwen.com/subject/jsntrxtx.html