代理传值 回调的使用
来源:互联网 发布:office教程 知乎 编辑:程序博客网 时间:2024/05/21 01:54
我以前曾今写过一篇博客,是讲解代理传值的,讲解的很不好,有许多没有讲清楚的地方,现在抽个时间将它讲解清楚,代理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
- 代理传值 回调的使用
- ios代理的使用,正向传值,逆向传值
- 委托和代理(类似接口回调的使用)
- iOS 代理的传值使用
- Swift 协议代理的使用和传值
- iOS学习笔记-----使用代理(Delegate)的页面传值
- 代理传值和 block 回调的步骤。
- NTLM代理的使用
- 代理的简单使用
- charles代理的使用
- iOS 代理的使用
- 代理的使用步骤
- iOS 代理的使用
- 动态代理的使用
- iOS代理的使用
- IOS 代理的使用
- iOS 使用代理实现页面传值
- iOS 使用代理实现页面传值
- URI和URL区别以及相对路径和绝对路径的区别
- 《Linux内核与实现》——定时器与时间管理
- ToolRunner机制
- Puppet Internals
- Spring的组件自动扫描机制
- 代理传值 回调的使用
- OC-文件夹/文件的创建
- Hadoop-2.2.0中文文档—— MapReduce 下一代 - Encrypted Shuffle
- 《Linux设备驱动程序》——中断处理
- 行人检测算法(ICF DPM)&CCV(A Morden Computer Vision Library)的使用&VisualBox下使用Ubuntu
- 使用SVN的时候右键里找不到checkout选项的解决方法
- JVM整理
- oracle中rownum和rowid的区别
- URL编码与解码