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

代理跟block

时间:2016-04-22 00:53:54      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

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

MainViewController

#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;
}

第二个界面:OtherViewController

#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.
}

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

MainViewController

#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];
}

第二个界面 :OtherViewController

#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];
}

 

代理跟block

标签:

原文地址:http://www.cnblogs.com/bella92/p/5419495.html

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