首页 > 代码库 > 代理&Block 传值

代理&Block 传值

刚学习苹果开发的时候不懂代理,Block,感觉好麻烦,就知道大概意思,现在回头看,发现就懂了,留一份自己的Demo希望初学的人能够看出点什么。

代理传值:在第二个界面输入值传给第一个界面

MainViewController

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController

@property (nonatomic, retain) UITextField *labelTwo;
@end

#import "MainViewController.h"
#import "OtherViewController.h"
@interface MainViewController ()<OtherViewDelegate>
{
    NSString *_str;
}
@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _labelTwo = [[UITextField alloc]initWithFrame:CGRectMake(160, 200, 70, 44)];
    _labelTwo.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_labelTwo];
    
    UIButton *buttonTow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonTow setTitle:@"" forState:UIControlStateNormal];
    buttonTow.frame = CGRectMake(160,100, 70, 44);
    [self.view addSubview:buttonTow];
    [buttonTow addTarget:self action:@selector(tuiPush) forControlEvents:UIControlEventTouchUpInside];
}

- (void)tuiPush
{
    OtherViewController *view = [[OtherViewController alloc] init];
    view.delegate = self;
    [self.navigationController pushViewController:view animated:YES];
}

- (void)OtherViewLabel:(UITextField *)labelOne
{
    _labelTwo.text = labelOne.text;
}

第二个界面:OtherViewController

#import <UIKit/UIKit.h>
@protocol OtherViewDelegate<NSObject>

- (void)OtherViewLabel:(UITextField *)labelOne;

@end

@interface OtherViewController : UIViewController

@property (nonatomic, retain) UITextField *labelOne;
@property (nonatomic, weak)id<OtherViewDelegate>delegate;
@end


#import "OtherViewController.h"

@interface OtherViewController ()

@end

@implementation OtherViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _labelOne = [[UITextField alloc]initWithFrame:CGRectMake(160, 200, 70, 44)];
    _labelOne.backgroundColor = [UIColor grayColor];
//    _labelOne.text = @"21312";
    [self.view addSubview:_labelOne];
    
    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonOne setTitle:@"传递" forState:UIControlStateNormal];
    buttonOne.frame = CGRectMake(160,100, 70, 44);
    [self.view addSubview:buttonOne];
    [buttonOne addTarget:self action:@selector(delegateM) forControlEvents:UIControlEventTouchUpInside];
}

- (void)delegateM
{
    [_delegate OtherViewLabel:_labelOne];
    
    [self.navigationController popViewControllerAnimated:YES];
}

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

下面是Block 传值  实现相同的功能

MainViewController

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController
@property (nonatomic, retain) UITextField *labelTwo;

@end
#import "MainViewController.h"
#import "OtherViewController.h"
@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _labelTwo = [[UITextField alloc]initWithFrame:CGRectMake(160, 200, 70, 44)];
    _labelTwo.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_labelTwo];
    
    // Do any additional setup after loading the view.
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(160, 100, 100, 44);
    [btn setTitle:@"chuan" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)btnClick
{
    OtherViewController *other = [[OtherViewController alloc] init];
    [other showTextFieldUsingBlock:^(UITextField *textOne) {
        _labelTwo.text = textOne.text;
    }];

    [self.navigationController pushViewController:other animated:YES];
}

第二个界面 :OtherViewController

#import <UIKit/UIKit.h>
typedef void (^MyBlock)(UITextField *textOne);

@interface OtherViewController : UIViewController
@property (nonatomic, retain)UITextField *textOne;
@property(nonatomic, strong) MyBlock block;
- (void)showTextFieldUsingBlock:(MyBlock)block;
@end

#import "OtherViewController.h"

@interface OtherViewController ()

@end

@implementation OtherViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _textOne = [[UITextField alloc]initWithFrame:CGRectMake(160, 200, 70, 44)];
    _textOne.backgroundColor = [UIColor grayColor];
    //    _labelOne.text = @"21312";
    [self.view addSubview:_textOne];
    
    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonOne setTitle:@"传递" forState:UIControlStateNormal];
    buttonOne.frame = CGRectMake(160,100, 70, 44);
    [self.view addSubview:buttonOne];
    [buttonOne addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
}


- (void)showTextFieldUsingBlock:(MyBlock)block
{
    self.block = block;
}


- (void)clickBtn
{
    if (self.block) {
        self.block(_textOne);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

两个代码就放到这里吧,希望能帮到你们