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





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章