天天看點

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