属性传值 Block传值

属性传值 就是将A页面的数据传到B页面上,下面就是将FirstViewController的TextField的内容传递到SecondViewController页面的导航栏标题和控制台输出上

#import

@interface FirstViewController :UIViewController

{

UITextField *tf;

}

@end

#import "FirstViewController.h"

#import "SecondViewController.h"//要将数据传到哪一个页面(ViewController)就引用那个头文件

- (void)viewDidLoad

{

[superviewDidLoad];

//定义一个按钮

UIButton* button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

button.frame=CGRectMake(100,100,100,100);

button.backgroundColor= [UIColorredColor];

[button addTarget:selfaction:@selector(doButton)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:button];

tf = [[UITextFieldalloc]initWithFrame:CGRectMake(10,300,100,40)];

tf.tintColor = [UIColororangeColor];

tf.backgroundColor = [UIColorgrayColor];

tf.tag =1000;

[self.viewaddSubview:tf];

}

-(void)doButton{

tf = (UITextField *)[self.viewviewWithTag:1000];

//push入栈引用计数+1,且控制权归系统

SecondViewController * seV =[[SecondViewControlleralloc]init];//将其实例化,否则找不到相应的属性

//直接属性传值

seV.naviTitle =tf.text;

seV.str [email protected]"传值成功";//属性(赋值)所要传的值要写在推出下一个窗口前面

[self.navigationControllerpushViewController:seVanimated:YES];

}

@end

#import

@interface SecondViewController :UIViewController

@property(nonatomic,strong)NSString * str;

@property (nonatomic,retain)NSString *naviTitle;

@end

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize naviTitle =_naviTitle;

- (void)viewDidLoad

{

[superviewDidLoad];

// Do any additional setup after loading the view.

self.view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,320,480)];

self.title =self.naviTitle;//只是为了显示第一页面传过来的内容将其显示到导航标题上

}

-(void)viewWillAppear:(BOOL)animated{

NSLog(@"%@",self.str);

NSLog(@"11---%@",self.naviTitle);

}

@end

Block传值

#import

@interface FirstViewController :UIViewController

{

UITextField *tf;

}

@property (nonatomic,retain)UILabel *label;

@end

#import "FirstViewController.h"

#import "SecondViewController.h"

@implementation FirstViewController

- (void)viewDidLoad

{

[superviewDidLoad];

//定义一个按钮

UIButton* button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

button.frame=CGRectMake(100,100,100,100);

button.backgroundColor= [UIColorredColor];

[button addTarget:selfaction:@selector(doButton)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:button];

//定义一个显示控件

self.label = [[UILabelalloc]initWithFrame:CGRectMake(0,400, 100, 40)];

self.label.backgroundColor = [UIColorgreenColor];

self.label.text = nil;//为了显示第二个视图控制器传过来的字符串

[self.viewaddSubview:self.label];

}

-(void)doButton{

tf = (UITextField *)[self.viewviewWithTag:1000];

//push入栈引用计数+1,且控制权归系统

SecondViewController * seV =[[SecondViewControlleralloc]init];//相对应的将其实例化,否则找不到相应的属性

//回调方法将输入框中的数据传输过来

[seVreturnText:^(NSString *showText) {

self.label.text = showText;

}];

[self.navigationControllerpushViewController:seV animated:YES];

}

@end

#import

typedefvoid (^ReturnTextBlock)(NSString *showText);//重新定义了一个名字

@interface SecondViewController :UIViewController

@property (nonatomic,retain)UITextField *tf;

@property (nonatomic,copy) ReturnTextBlock returnTextBlock;//定义的一个Block属性

- (void)returnText:(ReturnTextBlock)block;

@end

#import "SecondViewController.h"

- (void)viewDidLoad

{

[superviewDidLoad];

//定义一个输入框 将文字传给第一个界面,并且显示在UILabel上

self.tf = [[UITextFieldalloc]initWithFrame:CGRectMake(10,300, 100, 40)];

self.tf.tintColor = [UIColororangeColor];

self.tf.backgroundColor = [UIColorgrayColor];

[self.viewaddSubview:self.tf];

}

//在第一个界面传进来一个Block语句块的函数

//把传进来的Block语句块保存到本类的实例变量returnTextBlock(.h中定义的属性)中,然后寻找一个时机调用

-(void)returnText:(ReturnTextBlock)block{

self.returnTextBlock = block;

}

//而这个时机就是当视图将要消失的时候,需要重写:

-(void)viewWillDisappear:(BOOL)animated{

if (self.returnTextBlock !=nil) {

self.returnTextBlock(self.tf.text);

NSLog(@"self.tf.text %@",self.tf.text);

}

}

@end

时间: 2024-08-05 11:09:06

属性传值 Block传值的相关文章

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

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

页面传值:属性,协议,Block传值

1.属性传值和协议传值 1 #import "RootViewController.h" 2 #import "SecondViewController.h" 3 #warning 第四步:签订协议 4 @interface RootViewController ()<SecondViewControllerDelegate> 5 @property(nonatomic,strong)UILabel *showTextLable; 6 @end 7 8

整理一下 通知传值 Block传值

Block: 一. (1) 在需要传值的界面定义属性 // 点击collectionViewCell的回调 @property (nonatomic, copy) void(^DidcollectionClick)(NSIndexPath *indexPath); 在按钮或者手势 或者代理方法中 执行block - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPa

iOS block传值和属性传值

第一个控制器: -(void)barAction:(UIBarButtonItem*)sender{ NextViewController *next=[[NextViewController alloc]init];    //拿当前页面的值传到后一个页面    next.stringValue=self.rv.textField.text;//属性传值 //block传值    __weak RootViewController *weakSelf=self;//weakSelf可以在blo

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

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

代理和 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> //声明一个方法用来传值. //返回值表明传值的结果

UIButton的两种block传值方式

方式1 - 作为属性来传值 BlockView.h 与 BlockView.m // // BlockView.h // Block // // Created by YouXianMing on 15/1/14. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @class BlockView; /** 定义枚举值 */ typedef enum : NSUInteger

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笔记047-代理传值和block传值

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