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

//
//  ViewController.m
//  UI04_界面之間的通信
//
//  Created by lanou3g on 17/8/8.
//  Copyright ? 2017年 lanou3g. All rights reserved.
//

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

//遵守協(xié)議
@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];
}
//實現(xiàn)協(xié)議方法
- (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>


//委托方制定協(xié)議,代理人必須遵守
//協(xié)議
@protocol SecondViewControllerDelegate <NSObject>

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

@end

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

//代理的屬性  //用來設置代理,并且遵守協(xié)議
@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 {
    //利用代理去回傳數(shù)據(jù)
    [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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容