代理&Block 传值

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

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

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

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

时间: 2024-08-01 10:04:00

代理&Block 传值的相关文章

IOS传值--代理传值,block传值,NSNotificationCenter传值

一:利用代理传值,就是利用代理进行通信. 接口文件: #import <Foundation/Foundation.h> @protocol Cdelegate <NSObject> -(void)change:(NSString *)name; @end .h文件 @interface ViewController : UIViewController<Cdelegate> .m文件 - (IBAction)pushBB:(id)sender { BViewContr

iOS开发——UI篇&amp;代理/通知/Block传值(实现UItableView分组的收缩与展开)

代理/通知/Block传值实现UItableView分组的收缩与展开 初始化之后出现下面的界面 准备: 1:定义一个BOOL值用来记录点击 1 @property (nonatomic, assign, getter = isOpen) BOOL open; 2:在相应的点击方法里面是实现点击 1 self.group.open = !self.group.open; 3:在numberOfRowsInSection中返回的时候使用三木判断是否点击,并且实现伸缩与展开, 1 return mod

iOS开发——代理与block传值

一.代理传值的方法 1.Hehe1ViewController.h中 #import <UIKit/UIKit.h> @protocol Hehe1ViewControllerDelegate <NSObject> - (void)backValueWith:(NSString*)str; @end @interface Hehe1ViewController : UIViewController @property(nonatomic,weak) id delegate; @en

代理和 block 传值的使用

// // ZYViewController.h // BlockTest // // Created by yejiong on 14/11/2. // Copyright © 2014年 zzz. All rights reserved. // #import <UIKit/UIKit.h> //1.声明一个 delegate 协议. @protocol ZYViewControllerDelegate <NSObject> //声明一个方法用来传值. //返回值表明传值的结果

iOS 页面间传值 之 单例传值 , block 传值

ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同一个对象,对象的属性相同,所以可以用来传值. block 传值 与 代理传值相似,主要用于第二个页面向第一个页面传值,block 传值具体步骤: 在第二个页面: 1.声明: block typedef void(^SendMessagerBlock) (NSString *str); 2.创建方法:

属性传值,协议传值,block传值,单例传值四种界面传值方式

一.属性传值 对于属性传值而言,相对于其它的三种 方法来说,是最基础,最简单的一种 方法,但,属性传值 有很大的局限性,因为是适用于第一个界面向第二个界面传 值,第二个向第三个界面传值等等.N界面向N + 1界面传值.而在此基础上,必须知道跳转界面的明确位置及所要传的值的具体类型.在第二个界面中声明所要传值 类型的属性. @interface SecondViewController : UIViewController //声明一个字符串属性来保存第一个界面传过来的字符串内容 @propert

IOS笔记047-代理传值和block传值

在两个不同的控制器之间传递数据,可以使用代理传值或者block传值. 例子是一个简单通讯录. 主界面如下: 添加联系人界面 查看/编辑联系人界面:默认是查看模式,点击编辑后进入编辑模式 编辑模式 数据更新成功. 其中添加联系人界面的数据传递使用代理方式实现. 编辑联系人界面的数据传递使用block实现. 下面来看具体过程 1.整个界面搭建 在storyboard里拖拽四个控制器,其中联系人界面是一个UITableView.界面之间的跳转使用代码实现,但是要给每一个控制器指定一个标识.按功能分别指

ios block传值

iOS开发中的页面传值方式有很多种,最常见的是代理和属性传值方法,不过,block也可以传值,而且在一些特定的场合中,block传值会更简单,下面是一个我写的一个demo,大家可以参考一下. 在#import "AppDelegate.h"中,代码如下: 在第一个控制器中代码如下: 在第二个控制器的.h文件中代码如下: 在第二个控制器的.m文件中代码如下 以上就完成相应的两个界面之间的传值 版权声明:本文为博主原创文章,未经博主允许不得转载.

界面通信之block传值

block传值有两种方式 ?式?: 使?block属性实现回调传值 ?式?: 在?法中定义block实现回调传值 方式一比较便于理解而且常用,下面介绍方式一是如何传值的 使用block属性传值和代理传值的步骤基本上是一样的 typedef void(^BaDa)(NSString *, UIColor *); @interface SecondViewController : UIViewController @property (nonatomic, retain) UITextField *