iOS 属性传值 Block传值 两个ViewController之间的
2014-09-04 10:26
405 查看
属性传值 就是将A页面的数据传到B页面上,下面就是将FirstViewController的TextField的内容传递到SecondViewController页面的导航栏标题和控制台输出上
#import <UIKit/UIKit.h>
@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 =@"传值成功";//属性(赋值)所要传的值要写在推出下一个窗口前面
[self.navigationControllerpushViewController:seVanimated:YES];
}
@end
#import <UIKit/UIKit.h>
@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 <UIKit/UIKit.h>
@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 <UIKit/UIKit.h>
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
#import <UIKit/UIKit.h>
@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 =@"传值成功";//属性(赋值)所要传的值要写在推出下一个窗口前面
[self.navigationControllerpushViewController:seVanimated:YES];
}
@end
#import <UIKit/UIKit.h>
@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 <UIKit/UIKit.h>
@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 <UIKit/UIKit.h>
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
相关文章推荐
- IOS中,在两个ViewController之间传值
- IOS 两个ViewController 之间传值
- iOS两个viewController之间传值
- IOS学习笔记30—两个ViewController间传值(一)
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- IOS学习笔记30—两个ViewController间传值(一)
- 使用Delegate在两个ViewController之间传值
- iOS开发:StoryBoard 使用Block在两个界面之间传值
- iOS ViewController之间传值方法五(使用block)
- IOS—两个ViewController间传值
- IOS开发: 使用Block在两个界面之间传值(Block高级用法:Block传值)
- 怎么在两个ViewController之间传值
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- ios 页面之间的传值:属性传值,代理传值,block传值
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS 两个viewcontroller之间的delegate传值
- ios 两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- iOS开发:使用Block在两个界面之间传值
- iOS属性传值(视图控制器viewController的执行顺序的理解和懒人加载的理解)