iOS 属性传值 Block传值 两个ViewController之间的

属性传值 就是将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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值