我以前曾今写过一篇博客,是讲解代理传值的,讲解的很不好,有许多没有讲清楚的地方,现在抽个时间将它讲解清楚,代理delegate 使用他进行传值,其实他就类似于一个第三方授权一样,我要从A这个类中取出a这个值,我可以使用代理,让代理去执行取值
我这边写了两个类一个teacher类,一个student类,我这边为了更加明朗的显示出来,所以我将这两个类写成视图的格式,这样就能更直观的现实出来;类似于两个页面的传值;回调其实,回调和代理都可以实现两个页面的传值
我在teacher,中加入了一个按钮,和一个文本框,用来接受student页面传输过来的值;使用代理的时候一定要将代理传输过去,传递代理
代码如下,这个是teacher类的:
//
// teacherViewController.h
// dreamDemo
//
// Created by apple on 14-11-18.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void(^myBlock) (NSString *);
@protocol myDelegate <NSObject>
-(void)myBlick:(myBlock)block;
-(void)setText1:text;
@end
@interface teacherViewController : UIViewController<myDelegate>
@property(strong,nonatomic)UIButton *button;
@property(strong,nonatomic)UITextField *label;
@end
//
// teacherViewController.m
// dreamDemo
//
// Created by apple on 14-11-18.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import "teacherViewController.h"
#import "studentViewController.h"
@interface teacherViewController ()
@end
@implementation teacherViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self initView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)initView{
self.button=[UIButton buttonWithType:UIButtonTypeCustom];
self.button.backgroundColor=[UIColor blackColor];
self.button.frame=CGRectMake(50, 100, 120, 100);
[self.button setTitle:@"push" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(chuan) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
self.label=[[UITextField alloc]initWithFrame:CGRectMake(50, 230, 200, 100)];
self.label.backgroundColor=[UIColor redColor];
[self.view addSubview:self.label];
}
-(void)chuan{
studentViewController *viewController=[[studentViewController alloc]initWithNibName:nil bundle:nil];
viewController.delegate=self;
[self.navigationController pushViewController:viewController animated:YES];
}
-(void)setText1:text{
_label.text=text;
}
-(void)myBlick:(myBlock)block{
block(_label.text);
}
@end
//
// studentViewController.h
// dreamDemo
//
// Created by apple on 14-11-18.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "teacherViewController.h"
@interface studentViewController : UIViewController
@property id<myDelegate> delegate;
@property(strong,nonatomic)UIButton *button;
@property(strong,nonatomic)UITextField *label;
@property(strong,nonatomic)UILabel *huimie;
@end
//
// studentViewController.m
// dreamDemo
//
// Created by apple on 14-11-18.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import "studentViewController.h"
#import "teacherViewController.h"
@interface studentViewController ()
@end
@implementation studentViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self initView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)initView{
self.button=[UIButton buttonWithType:UIButtonTypeCustom];
self.button.backgroundColor=[UIColor redColor];
self.button.frame=CGRectMake(50, 100, 120, 100);
[self.button setTitle:@"返回" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
self.label=[[UITextField alloc]initWithFrame:CGRectMake(50, 230, 200, 100)];
self.label.backgroundColor=[UIColor yellowColor];
[self.view addSubview:self.label];
self.huimie=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
self.huimie.backgroundColor=[UIColor yellowColor];
[self.view addSubview:self.huimie];
}
-(void)fanhui{
[_delegate setText1:_label.text];
[_delegate myBlick:^(NSString *string) {
_huimie.text=string;
}];
[self.navigationController popViewControllerAnimated:YES];
}
@end