代理传值

代理(delegate)传值 ---- 顾名思义就是委托别人办事,就是当一件事情发生后,自己不处理,让别人来处理。

//代理传值
注意:从后往前传(如果从前往后传,会传不成功)
//流程:
1.后一个界面定义一个协议,并且定义一个属性叫delegate
2.在前一个界面进入后一个界面的瞬间,(即:创建完成一个界面之后),让前一个界面作为后一个界面的delegate
3.前一个界面实现代理方法
4.后一个界面在合适的机会, 让代理, 执行,代理方法 (传的值以参数的形式 含在代理方法里)

代码如下:

#import "FirstViewController.h"
#import "SecondViewController.h"
#import "UIButton+Create.h"
@interface FirstViewController ()
{
    UILabel * _label;
}
@end

@implementation FirstViewController
- (void)dealloc
{
    [_label release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"首页";
    self.view.userInteractionEnabled = YES;
    /**
     *  1.创建一个UIButton,
     *  2.并添加响应事件,从首页跳转到第二个页面.
     */
    UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Push" target:self action:@selector(didClickButtonAction)];
    button.userInteractionEnabled = YES;
    [self.view addSubview:button];
    
    
    
    /**
     *  1.在第1个界面创建一个UILabel
     *  2.把第二页输入框输入的字符串,通过代理方法传过来
     *  3.然后通过赋值给UILabel
     */
    _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
    _label.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_label];
    
	// Do any additional setup after loading the view.
}

- (void)didClickButtonAction
{
    
    /**
     *  1.用push的方法推出下一个页面
     *  2.把第二页输入框输入的字符串,通过代理方法传过来
     *  3.从而实现把首页输入框输入的字符串,传到第二页的UILabel上.
     */
    SecondViewController * secondVC = [[SecondViewController alloc]init];
    secondVC.delegate = self;//确认代理
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
}

//实现代理方法
- (void)passByValueSecondVC:(SecondViewController *)secondVC text:(NSString *)text
{
    _label.text = text;
}


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

@end

#import <UIKit/UIKit.h>
@class SecondViewController;
@protocol secondViewControllerDelegate <NSObject>

- (void)passByValueSecondVC:(SecondViewController *)secondVC text:(NSString *)text;

@end

@interface SecondViewController : UIViewController
@property (nonatomic,assign)id<secondViewControllerDelegate>delegate;
@end


#import "SecondViewController.h"
#import "UIButton+Create.h"
#import "FirstViewController.h"
@interface SecondViewController ()
{
    UITextField * _textField;//创建一个输入框
}
@end
@implementation SecondViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.title = @"第二页";
    /**
     *  1.创建一个UIButton,
     *  2.并添加响应事件,从第二个页面返回到首页.
     */
    UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Back" target:self action:@selector(didClickButtonAction)];
    [self.view addSubview:button];
    
    /**
     *  1.在第二个界面创建一个输入框
     *
     */
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textField];
    
    
    
	// Do any additional setup after loading the view.
}

- (void)didClickButtonAction
{
    //调用代理方法,_textField.text传值
    [_delegate passByValueSecondVC:self text:_textField.text];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

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

@end



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值