在IOS开发中,时常会用到传值, 之前学过属性传值,函数传值也称方法传值, 现在又学了协议传值, 目前学的协议传值指两个类之间传值, 这里我用简单的例子记录一下协议传值和函数传值. (本人才疏学浅, 如若有错误, 还望大神能够赐教,本人定会感激不尽.)
delegate传值有六步
/*
* 1. 声明协议
* 2. 设置代理人属性
* 3. 向代理人发信息, 执行相应的方法
* 4. 签订协议
* 5. 指定代理人
* 6. 实现协议方法,
*/
主页里面有一个UITextField 和 UIButton , 在TextField 里面输入字符, 点击 button 后, 可以将textField 里面的数据传到SecondController 里面,这个用的是函数传值.
#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()<SecondViewControllerDelegate>
#warning 协议第四步:签订协议
@property(nonatomic, retain) UITextField *textfield;
@property(nonatomic, retain) UILabel *lable;
@end
@implementation MainViewController
-(void)dealloc
{
[_lable release];
[_textfield release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createButton];
}
-(void)createButton
{
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 300, 100, 50)];
btn.backgroundColor = [UIColor blueColor];
[btn setTitle:@"确定" forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[btn release];
_textfield = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 200, 80)];
_textfield.borderStyle = 3;
[self.view addSubview:_textfield];
[_textfield release];
_lable = [[UILabel alloc] initWithFrame:CGRectMake(150, 300, 100, 50)];
_lable.backgroundColor = [UIColor brownColor];
[self.view addSubview:_lable];
[_lable release];
}
-(void)btnAction:(UIButton *)button
{
#warning 协议第五步: 指定代理人
SecondViewController *second = [[SecondViewController alloc]init];
// 调用函数
[second setTextString:_textfield.text];
[second setString:_textfield.text];//函数传值
second.delegate = self;
[self.navigationController pushViewController:second animated:YES];
[second release];
}
#warning 协议第六步: 实现协议方法
-(void)passValue:(NSString *)string
{
_lable.text = string;
}
@end
SecondControllView.h 代码 , 先声明 一个协议
#import <UIKit/UIKit.h>
#warning 协议第 1 步: 声明协议
@protocol SecondViewControllerDelegate <NSObject>
-(void) passValue:(NSString *)string;
@end
@interface SecondViewController : UIViewController
#warning 协议第 2 步: 设置代理属性
@property(nonatomic, assign)id<SecondViewControllerDelegate>delegate;
// 设置传值的函数
-(void)setTextString:(NSString *)string;
-(void)setString:(NSString *)string;
@end
SecondControllView.m 代码 , 有 UITextField 和 UIButton 两个控件, 这个里面用的是delegate 传值, 在 TextField 输入数据,点击按钮,可以将 TextField 值传到MainController 里面
#import "SecondViewController.h"
@interface SecondViewController ()
// 设置接受值
@property(nonatomic, copy)NSString *string;
@property(nonatomic, retain)UITextField *textFiled;
@end
@implementation SecondViewController
-(void)dealloc
{
[_string release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createButton];
}
-(void)createButton
{
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 300, 100, 50)];
btn.backgroundColor = [UIColor lightGrayColor];
[btn setTitle:@"第一页" forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[btn release];
_textFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 200, 80)];
_textFiled.borderStyle = 3;
// _textFiled.text = _string;
[self.view addSubview:_textFiled];
[_textFiled release];
}
-(void)btnAction:(UIButton *)button
{
[self.delegate passValue:_textFiled.text];//*************
[self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)setTextString:(NSString *)string
{
self.string = string;
}
-(void)setString:(NSString *)string
{
_string = string;
// 不能写成,否则会循环引用
// self.string = string;
}
@end