码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——代理与block传值

时间:2016-03-26 12:13:43      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

  一、代理传值的方法

  1.Hehe1ViewController.h中

#import <UIKit/UIKit.h>

 

@protocol Hehe1ViewControllerDelegate <NSObject>

 

- (void)backValueWith:(NSString*)str;

 

@end

 

@interface Hehe1ViewController : UIViewController

@property(nonatomic,weak) id delegate;

@end

  2.Hehe1ViewController.m中

#import "Hehe1ViewController.h"

 

@interface Hehe1ViewController ()

@property(nonatomic,strong) UITextField *heheTextField;

@end

 

@implementation Hehe1ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    UITextField *heheTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];

    self.heheTextField = heheTextField;

    heheTextField.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:heheTextField];

    

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];

    button1.frame = CGRectMake(20, 250, 300, 50);

    button1.backgroundColor = [UIColor orangeColor];

    [button1 setTitle:@"Back to VC" forState:UIControlStateNormal];

    [button1 setTintColor:[UIColor whiteColor]];

    [button1 addTarget:self action:@selector(goToHehe1) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button1];

}

 

- (void)goToHehe1 {

    NSLog(@"hehe1...");

    

    [_delegate backValueWith:self.heheTextField.text];

    

    [self.navigationController popViewControllerAnimated:YES];

}

@end

 

  二、block传值

  1.Hehe2ViewController.h中

#import <UIKit/UIKit.h>

 

typedef void(^BackValue)(NSString *str);

 

@interface Hehe2ViewController : UIViewController

@property(nonatomic,copy) BackValue backValue;

 

- (void)returnStr:(BackValue)block;

 

@end

 

  2.Hehe2ViewController.m中

#import "Hehe2ViewController.h"

 

@interface Hehe2ViewController ()

@property(nonatomic,strong) UITextField *heheTextField;

@end

 

@implementation Hehe2ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    UITextField *heheTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];

    self.heheTextField = heheTextField;

    heheTextField.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:heheTextField];

    

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];

    button2.frame = CGRectMake(20, 250, 300, 50);

    button2.backgroundColor = [UIColor orangeColor];

    [button2 setTitle:@"Back to VC" forState:UIControlStateNormal];

    [button2 setTintColor:[UIColor whiteColor]];

    [button2 addTarget:self action:@selector(goToHehe2) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button2];

}

 

- (void)returnStr:(BackValue)block {

    self.backValue = block;

}

 

- (void)goToHehe2 {

    NSLog(@"hehe2...");

    

    self.backValue(self.heheTextField.text);

    

    [self.navigationController popViewControllerAnimated:YES];

}

 

@end

 

  三、在ViewController.m中接收传回的值

#import "ViewController.h"

#import "Hehe1ViewController.h"  //delegate

#import "Hehe2ViewController.h"  //block

 

#define klSceenWidth self.view.bounds.size.width

 

@interface ViewController ()

@property(nonatomic,strong) UILabel *label1;

@property(nonatomic,strong) UILabel *label2;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, klSceenWidth-40, 30)];

    self.label1 = label1;

    [self.view addSubview:label1];

    

  //delegate

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];

    button1.frame = CGRectMake(20, 150, klSceenWidth-40, 30);

    button1.backgroundColor = [UIColor orangeColor];

    [button1 setTitle:@"go to hehe1" forState:UIControlStateNormal];

    [button1 setTintColor:[UIColor whiteColor]];

    [button1 addTarget:self action:@selector(goToHehe1) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button1];

    

  //block

    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, klSceenWidth-40, 30)];

    self.label2 = label2;

    [self.view addSubview:label2];

 

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];

    button2.frame = CGRectMake(20, 250, klSceenWidth-40, 30);

    button2.backgroundColor = [UIColor orangeColor];

    [button2 setTitle:@"go to hehe1" forState:UIControlStateNormal];

    [button2 setTintColor:[UIColor whiteColor]];

    [button2 addTarget:self action:@selector(goToHehe2) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button2];

}

- (void)backValueWith:(NSString*)str {

    self.label1.text = str;

}

 

- (void)goToHehe1 {

    Hehe1ViewController *heheVC1= [[Hehe1ViewController alloc] init];

    heheVC1.delegate = self;

    [self.navigationController pushViewController:heheVC1 animated:YES];

}

- (void)goToHehe2 {

    Hehe2ViewController *heheVC2= [[Hehe2ViewController alloc] init];

    [heheVC2 returnStr:^(NSString *str) {

        self.label2.text = str;

    }];

    [self.navigationController pushViewController:heheVC2 animated:YES];

}

@end

 

iOS开发——代理与block传值

标签:

原文地址:http://www.cnblogs.com/yyt-hehe-yyt/p/5322538.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!