iOS 屬性傳值 Block傳值 兩個ViewController之間的

來源:互聯網
上載者:User

iOS 屬性傳值 Block傳值 兩個ViewController之間的

屬性傳值 就是將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 =@"傳值成功";//屬性(賦值)所要傳的值要寫在推出下一個視窗前面

[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:seVanimated: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





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.