iOS 頁面間幾種傳值方式(屬性,代理,block,單例,通知)

來源:互聯網
上載者:User

標籤:

第二個介面中的lable顯示第一個介面textField中的文本

首先我們建立一個RootViewControllers和一個DetailViewControllers,在DetailViewControllers中聲明一個textString屬性,用於接收傳過來的字串,

 

同時建立一個Lable用來顯示傳過的字串

 

 

在RootViewControllers上引入DetailViewControllers同時聲明一個textField屬性用來輸入字串

 

然後在RootViewControllers上我們建立並添加一個button,當點擊button時響應相應方法進行視圖間的切換完成視圖間的傳值

 

 

(二)Block傳值

block傳值也是從第二個介面給第一個介面傳值

首先我們在DetailViewcontrollers的.h檔案中,屬性

 

在RootViewControllers的.m檔案中,其他不變,在button的回應程式法裡我們為block屬性賦值完成block傳值

 

 

 

(三)代理傳值

RootViewControllers頁面push到DetailViewControllers頁面,如果DetailViewControllers頁面的資訊想回傳(回調)到RootViewControllers頁面,用代理傳值,其中DetailViewControllers定義協議和聲明代理,RootViewControllers確認並實現代理,RootViewControllers作為DetailViewControllers的代理

首先在DetailViewControllers.h檔案中我們建立協議方法

 

在DetailViewControllers的.m中我們判定代理對象存在時,為其綁定相應方法

 

RootViewControllers的.m檔案中我們指定代理並讓其執行代理的方法

 

 

 

(四)單例傳值

 

單例傳值(實現共用)

AppStatus.h  建立一個單例類 AppStatus

 1 #import <Foundation/Foundation.h> 2  3 @interface AppStatus : NSObject 4 { 5     NSString *_contextStr; 6 } 7  8 @property(nonatomic,retain)NSString *contextStr; 9 10 +(AppStatus *)shareInstance;11 12 @end

AppStatus.m

 1 #import "AppStatus.h" 2  3 @implementation AppStatus 4  5 @synthesize contextStr = _contextStr; 6  7 static AppStatus *_instance = nil; 8  9 +(AppStatus *)shareInstance10 {11     if (_instance == nil)12     {13         _instance = [[super alloc]init];14     }15     return _instance;16 }17 18 -(id)init19 {20     if (self = [super init])21     {22         23     }24     return self;25 }26 27 -(void)dealloc28 {29     [super dealloc];30 }31 32 @end

RootViewController.h

 1 #import "RootViewController.h" 2 #import "DetailViewController.h" 3 #import "AppStatus.h" 4  5 @interface RootViewController () 6  7 @end 8  9 @implementation RootViewController10 11 -(void)loadView12 {13     //核心代碼 14     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];15     btn.frame = CGRectMake(0, 0, 100, 30);16     [btn setTitle:@"Push" forState:0];17     [btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];18     [self.view addSubview:btn];19 }20 21 -(void)pushAction:(id)sender22 {23     tf = (UITextField *)[self.view viewWithTag:1000];24 25  //單例傳值  將要傳遞的資訊存入單例中(共用中)26   //  [[AppStatus shareInstance]setContextStr:tf.text]; 跟下面這種寫法是等價的27     [AppStatus shareInstance].contextStr = tf.text;28     //導航push到下一個頁面29     //pushViewController 入棧引用計數+1,且控制權歸系統30     DetailViewController *detailViewController = [[DetailViewController alloc]init];31 32     //導航push到下一個頁面33     [self.navigationController pushViewController:detailViewController animated:YES];34     [detailViewController release];35 } 36 37 @end

DetailViewController.h

1 #import <UIKit/UIKit.h>2 @protocol ChangeDelegate;//通知編譯器有此代理3 4 @interface DetailViewController : UIViewController5 {6     UITextField *textField;7 }8 9 @end

DetailViewController.m

 1 #import "DetailViewController.h" 2 #import "AppStatus.h" 3  4 @interface DetailViewController () 5  6 @end 7  8 @implementation DetailViewController 9 10 @synthesize naviTitle = _naviTitle;11 12 -(void)loadView13 {14     self.view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];15 16     //單例17     self.title = [AppStatus shareInstance].contextStr;18     textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];19     textField.borderStyle = UITextBorderStyleLine;20     [self.view addSubview:textField];21     [textField release];22 23     UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)];24     self.navigationItem.rightBarButtonItem = doneItem;25     [doneItem release];26 }27 28 //這個方法是執行多遍的  相當於重新整理view29 -(void)viewWillAppear:(BOOL)animated30 {31     [super viewWillAppear:animated];32     tf = (UITextField *)[self.view viewWithTag:1000];33     tf.text = [AppStatus shareInstance].contextStr;34 }35 36 //pop回前一個頁面37 -(void)doneAction:(id)sender38 {39     //單例傳值40     [AppStatus shareInstance].contextStr = textField.text;41     [self.navigationController popToRootViewControllerAnimated:YES];42 } 
(五)通知傳值

誰要監聽值的變化,誰就註冊通知  特別要注意,通知的接受者必須存在這一先決條件

1、註冊通知

2、通知中樞發送一條訊息通知,其中name前後一定要一樣

3、實現通知中樞內部的方法,並實現傳值

4、訊息發送完,要移除掉。(頁面將要消失的時候)

A頁面RootViewController.m

 

 

SecondViewController.m

 

 

iOS 頁面間幾種傳值方式(屬性,代理,block,單例,通知)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.