Delegate传值

在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
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值