码迷,mamicode.com
首页 > 其他好文 > 详细

代理&Block 传值

时间:2014-05-09 16:51:06      阅读:394      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

刚学习苹果开发的时候不懂代理,Block,感觉好麻烦,就知道大概意思,现在回头看,发现就懂了,留一份自己的Demo希望初学的人能够看出点什么。

代理传值:在第二个界面输入值传给第一个界面

MainViewController

bubuko.com,布布扣
#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController

@property (nonatomic, retain) UITextField *labelTwo;
@end

#import "MainViewController.h"
#import "OtherViewController.h"
@interface MainViewController ()<OtherViewDelegate>
{
    NSString *_str;
}
@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _labelTwo = [[UITextField alloc]initWithFrame:CGRectMake(160, 200, 70, 44)];
    _labelTwo.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_labelTwo];
    
    UIButton *buttonTow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonTow setTitle:@"" forState:UIControlStateNormal];
    buttonTow.frame = CGRectMake(160,100, 70, 44);
    [self.view addSubview:buttonTow];
    [buttonTow addTarget:self action:@selector(tuiPush) forControlEvents:UIControlEventTouchUpInside];
}

- (void)tuiPush
{
    OtherViewController *view = [[OtherViewController alloc] init];
    view.delegate = self;
    [self.navigationController pushViewController:view animated:YES];
}

- (void)OtherViewLabel:(UITextField *)labelOne
{
    _labelTwo.text = labelOne.text;
}
bubuko.com,布布扣

第二个界面:OtherViewController

bubuko.com,布布扣
#import <UIKit/UIKit.h>
@protocol OtherViewDelegate<NSObject>

- (void)OtherViewLabel:(UITextField *)labelOne;

@end

@interface OtherViewController : UIViewController

@property (nonatomic, retain) UITextField *labelOne;
@property (nonatomic, weak)id<OtherViewDelegate>delegate;
@end


#import "OtherViewController.h"

@interface OtherViewController ()

@end

@implementation OtherViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _labelOne = [[UITextField alloc]initWithFrame:CGRectMake(160, 200, 70, 44)];
    _labelOne.backgroundColor = [UIColor grayColor];
//    _labelOne.text = @"21312";
    [self.view addSubview:_labelOne];
    
    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonOne setTitle:@"传递" forState:UIControlStateNormal];
    buttonOne.frame = CGRectMake(160,100, 70, 44);
    [self.view addSubview:buttonOne];
    [buttonOne addTarget:self action:@selector(delegateM) forControlEvents:UIControlEventTouchUpInside];
}

- (void)delegateM
{
    [_delegate OtherViewLabel:_labelOne];
    
    [self.navigationController popViewControllerAnimated:YES];
}

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

下面是Block 传值  实现相同的功能

MainViewController

bubuko.com,布布扣
#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController
@property (nonatomic, retain) UITextField *labelTwo;

@end
#import "MainViewController.h"
#import "OtherViewController.h"
@interface MainViewController ()

@end

@implementation MainViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _labelTwo = [[UITextField alloc]initWithFrame:CGRectMake(160, 200, 70, 44)];
    _labelTwo.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_labelTwo];
    
    // Do any additional setup after loading the view.
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(160, 100, 100, 44);
    [btn setTitle:@"chuan" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)btnClick
{
    OtherViewController *other = [[OtherViewController alloc] init];
    [other showTextFieldUsingBlock:^(UITextField *textOne) {
        _labelTwo.text = textOne.text;
    }];

    [self.navigationController pushViewController:other animated:YES];
}
bubuko.com,布布扣

第二个界面 :OtherViewController

bubuko.com,布布扣
#import <UIKit/UIKit.h>
typedef void (^MyBlock)(UITextField *textOne);

@interface OtherViewController : UIViewController
@property (nonatomic, retain)UITextField *textOne;
@property(nonatomic, strong) MyBlock block;
- (void)showTextFieldUsingBlock:(MyBlock)block;
@end

#import "OtherViewController.h"

@interface OtherViewController ()

@end

@implementation OtherViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _textOne = [[UITextField alloc]initWithFrame:CGRectMake(160, 200, 70, 44)];
    _textOne.backgroundColor = [UIColor grayColor];
    //    _labelOne.text = @"21312";
    [self.view addSubview:_textOne];
    
    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonOne setTitle:@"传递" forState:UIControlStateNormal];
    buttonOne.frame = CGRectMake(160,100, 70, 44);
    [self.view addSubview:buttonOne];
    [buttonOne addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
}


- (void)showTextFieldUsingBlock:(MyBlock)block
{
    self.block = block;
}


- (void)clickBtn
{
    if (self.block) {
        self.block(_textOne);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
bubuko.com,布布扣

两个代码就放到这里吧,希望能帮到你们

代理&Block 传值,布布扣,bubuko.com

代理&Block 传值

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/wuwangchuxin/p/3708685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!