【轉】使用storyboard實現頁面跳轉,簡單的資料傳遞

來源:互聯網
上載者:User

標籤:

由於最近才接觸到IOS,蘋果已經建議storyboard來搭建所有介面了,於是我也追隨時尚,直接開始使用storyboard。(不料在涉及到頁面跳轉的時候,遇到的問題是:點擊後沒有任何反應)眾所周知,在storyboard中,介面的跳轉是通過segue來實現的,利用它,省去了方法內寫入跳轉的代碼。

 

一 視圖跳轉

《StoryBoard下的視圖跳轉》

我們知道:segue共有三種類型:push,modal,和custom。如://01

簡單說下這三個的作用:使用導覽列壓進新的控制器(push),模態的載入視圖控制器(modal),自訂(custom)。

好了,廢話少說,現在開始我們的旅行。

1,首先建立一個Single View 模板的項目,記得勾選上storyboard。然後開啟它,在rootViewController(也就是我們的主視圖)添加一些label和一個button。

2,在右邊的庫中拖入一個ViewController,也添加一個Label。具體如所示://02

3,選中button,右鍵(或control+滑鼠左鍵)拖拽到第二個ViewController中,選擇:Modal,然後記得save。這個時候,運行模擬器,點擊button,你會發現成功跳轉到了第二個介面。我們沒有在代碼區做任何操作,甚至連button和第二個ViewController都沒有建立,確實就是這麼的簡單。//03

好了,到了這裡,簡單說一下storyboard下,利用segue介面跳轉一共有兩種方式:

第一種就是以上我的例子,利用button組件,拖拽添加segue,直接運行就可以用。

第二種是利用ViewController與ViewController之間,拖拽添加segue。不過,這種方法就需要在相應需要跳轉的方法內寫入代碼,手動去設定它的跳轉。

4,把剛才例子設定button的segue刪除(右鍵,點X),一切恢複了最初時的狀態,我們給buttom添加一個點擊的方法,然後在ViewController.m中實現此方法,在方法體內寫入這樣的代碼://04

5,注意看方法參數:@"second",這個second是我自命名的一個標示符,一會你就會遇到。

6,save儲存,開啟storyboard,選中rootViewController,右鍵拖拽到第二個ViewController,在彈出的介面同樣選擇:modal。//05

7,開啟這個segue的設定頁面:設定其identifier為second,就是我在代碼中的那個自具名引數。

8,save儲存,運行模擬器,你會驚奇的發現,實現了同樣的跳轉。

到現在為止,我們一共用兩種簡單的方式實現了視圖的跳轉:1是設定button的segue,2是設定viewcontroller與viewcontroller之間的segue,只是後者需要在代碼中手動管理跳轉。

看似很簡單的事情,卻讓我耽誤一些時間,主要是因為我在網上看的好多例子都是以UINavigationController為rootviewController(這樣省事省時,跳轉後還可以利用系統為我們建立的返回按鈕返回到rootViewController),然後用button拖拽到第二個視圖時選擇的push,由於當時不理解push相關的類型含義,所以在寫的時候,我總是選擇push,造就了點擊後無法跳轉。現在終於明朗了,記錄下來,供不明白的同學學習。

 

 

----------------------------------------------------------------------------------------------------

 //根據 segue Identifier跳轉介面

    [self performSegueWithIdentifier:@"GotoTwo" sender:self];

    

   //以modal 方式跳轉

    [self presentModalViewController:nil animated:YES];

    

   //壓進一個viewcontroller

    [self.navigationController pushViewController:nil animated:YES];

 

   //彈出一個viewcontroller  相當與返回上一個介面

    [self.navigationController popViewControllerAnimated:YES];

    

   // 以 modal跳轉的返回方法

    [self dismissModalViewControllerAnimated:YES];

-----------------------------------------------------------------------------------------------------

再寫一下關於segue三個類型的詳解:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

在storyboard中,segue有幾種不同的類型,在iphone和ipad的開發中,segue的類型是不同的。
在iphone中,segue有:push,modal,和custom三種不同的類型,這些類型的區別在與新頁面出現的方式。
而在ipad中,有push,modal,popover,replace和custom五種不同的類型。
 
 
modal 
最常用的情境,新的情境完全蓋住了舊的那個。使用者無法再與上一個情境互動,除非他們先關閉這個情境。
是在viewController中的標準切換的方式,包括淡出什麼的,可以選切換動畫。
Modalview:就是會彈出一個view,你只能在該view上操作,而不能切換到其他view,除非你關閉了modalview.
Modal View對應的segue type就是modal segue。
*Modal:Transition to another scene for the purposes of completing a task.當user在彈出的modalview裡操作完後,就應該dismiss the modal view scene然後切換回the originalview.
 

push
Push類型一般是需要頭一個介面是個Navigation Controller的。
是在navigation View Controller中下一級時使用的那種從右側劃入的方式
*Push:Create a chain of scenes where the user can move forward or back.該segue type是和navigation viewcontrollers一起使用。
 
popover(iPad only)
popover 類型,就是採用浮動窗的形式把新頁面展示出來
*Popover(iPad only):Displays the scene in a pop-up “window” over top of the current view.
 
Replace (iPad only):
替換當前scene,
Replace the current scene with another. This is used in some specialized iPad viewcontrollers (e.g. split-view controller).
 
custom
就是自訂跳轉方式啦。
*Custom:Used for programming a customtransition between scenes.
在Storyboard中使用自訂的segue類型

參考http://ryan.easymorse.com/?p=72

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

《Xib下的視圖跳轉》

現在說一下,沒有使用Storyboard,直接建立xib時的頁面跳轉,其實也很簡單,只要理解了,都不是問題。我也是從java剛轉過來,起初感覺很不適應,但是現在發現interface builder真的是太強大了。

 

1,建立一個項目,我用的是Empty Application模版,這種模版建立出來的項目只包含一個Appdelegate.h和Appdelegate.m檔案,rootviewController需要我們自行建立。(注意:最新的版本,apple把MainWindow.xib檔案取消了,所以無法開啟xib查看包含的表徵圖)此時運行模擬器只會顯示空白的介面。好了,File-newFile 建立一個Object-C class,開啟後,subclass of 選擇預設的UIViewController,注意,需要勾選上With XIB for user interface,不然一會無法建立主視圖。

2,建立好後,開啟其xib檔案,簡單添加一些button組件。此時儲存運行程式還是無法顯示我們建立的RootViewController。【有時如果你在項目清單下的user interface 選擇此MainViewController運行後台還會報:Applications are expected to have a root view controller at the end of application launch的錯誤,原因都是項目的Delegate無法發現一個rootViewContrller】所以這就需要我們手動在AppDelegate中讓它們關聯起來。

AppDelegate.h

AppDelegate.m

[self.viewController就是我們在.h檔案中聲明的]

 

3,好了,儲存後,這個時候運行就能顯示我們的介面了~同樣也很簡單吧!好了,我們現在參照以上的方法,再建立一個SecondViewController(記得勾選xib),然後我們給rootviewcontroller視圖的 button添加一個事件方法,可以讓它點擊後跳轉到SecondViewController。

(首先建立了一個SecondViewController並執行個體化,intitWithNibName的參數一定要正確寫控制器配對的xib檔案的名稱,調用presentModalViewController:controller 就可以跳轉啦。)

 

發現了把,其實都挺簡單的,本來可以建立single View Application模板的,這樣就省去建立rootViewController和在Delegate中寫那些代碼了,我這樣也就是讓大家感受一下,xib,delagate,stroyboard之間的區別和聯絡,都嘗試一下,你們就會明白他們之間的機制,至少可以會寫介面和跳轉的方法啦。哈哈,有什麼問題大家一起交流,我也是初學者,可以給我留言噢。~~~

 

 

 

二 頁面傳值

 

方法一使用segue傳遞資料,繼續上面的項目例子。

1,在rootViewController聲明一個UITextField並與storyboard關聯上。File-add添加一個SecondViewController(繼承UIViewController),然後在keyboard第二個viewcontroller的屬性設定其class關聯。同樣在SecondViewController聲明一個UITextField並關聯。://06

2,然後在rootViewController.m的檔案中覆蓋名稱為:prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender的方法,並寫入如下語句,//07

3,我們擷取了主視圖的文字框內容,並通過segue的方法發送了出去,接下來要做的就是在secondViewController聲明一個@property NSString * 類型,名稱為data的字串,然後在其.m檔案中的-(void) viewDidLoad方法中寫入如下代碼://08

 

注意:發送資料時,[send setValue:msg forKey:@"data"]; 這個"data"名稱一定要與跳轉後介面的聲明的類型對象的命名一致,不然的話,跳轉後的介面是收不到傳遞的值的。

 

 

 

方法二,使用notification廣播實現視圖跳轉傳遞資料,繼續上面的項目展開。

 

廣播機制分為:註冊----發送------------接收(接收方),具體請看一下代碼。

1,在要發送資料的視圖頁面.m檔案處理髮送邏輯的方法裡註冊+發送

 

[java] view plaincopy 
  1. - (IBAction)pressed:(id)sender {  
  2.       
  3. //    [self performSegueWithIdentifier:@"second" sender:self];  
  4.     NSLog(@"send message:%@",firstField.text);  
  5.   
  6.       
  7.       
  8.     //頁面跳轉傳值方法二:利用notification  
  9.     NSDictionary *dicts = [NSDictionary dictionaryWithObjectsAndKeys:@"one1",@"one",@"two2",@"two",@"three3",@"three", nil];  
  10.     //註冊(第一步)  
  11.     NSNotification *notification  =[NSNotification notificationWithName:@"mynotification" object:firstField.text];  
  12.     //發送(第二步)  
  13.     [[NSNotificationCenter defaultCenter] postNotification:notification];  
  14.       
  15.     //註冊+發送也可以一行完成(等效於以上兩行)  
  16.     [[NSNotificationCenter defaultCenter] postNotificationName:@"mynotification2" object:dicts];//發送一個字典過去  
  17.   
  18.   
  19. }  

notificationWithName:參數的值是自己定義,接收方以此名稱為接收標識。

 

2,在跳轉後,接收資料檢視頁面.m檔案中處理邏輯的方法裡 接收

 

[java] view plaincopy 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view.  
  5.       
  6.     //接受端:接受(第一步)  
  7.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationHandler:) name:@"mynotification" object:nil];  
  8.       
  9.       
  10.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationHandler2:) name:@"mynotification2" object:nil];  
  11.   
  12. }  
  13.   
  14. //自訂接收資訊和處理的方法(第二步)  
  15. -(void) notificationHandler:(NSNotification *) notification{  
  16.       
  17.     secondField.text = [notification object];//收到訊息後在UItextField中顯示出來  
  18.   
  19. }  
  20. //自訂接收字典資訊的方法  
  21. -(void) notificationHandler2:(NSNotification *) notification2{  
  22.   
  23.     NSDictionary *dict = [notification2 object];  
  24.      NSLog(@"receive dict :%@,forkey:%@",dict,[dict objectForKey:@"one"]);  
  25.   
  26. }  

 

注意:如果註冊的notification在目標視圖沒有收到或名稱寫錯,目標視圖的相關方法就不會執行

國外參考論壇【傳送門】http://blog.isotoma.com/2009/11/on-objective-c-delegates-and-nsnotification-objects/

 

方法三,通過Delegate委託傳遞資料

此方法我測試完後,感覺不是太好用,有一些局限性,相當於自訂讀取方法:無非是在A對象裡儲存了一個B對象的指標,然後在A的某個函數裡去設定B對象某個屬性的值。

具體看教程把。

1,首先add a File--  Objective-C protocol,然後聲明一個傳遞數值的方法:

 

[java] view plaincopy 
  1. //<1>自訂一個用來傳遞數值的delegate  
  2. @protocol ViewPassValueDelegate <NSObject>  
  3.   
  4.   
  5. -(void) passValue :( NSString *) value;  
  6.   
  7.   
  8.   
  9. @end  

 

 

2,然後在要發送資料的視圖的.h檔案下聲明一個自訂的delegate

 

[java] view plaincopy 
  1. #import <UIKit/UIKit.h>  
  2. #import "ViewPassValueDelegate.h"  
  3.   
  4.   
  5. @interface ViewController : UIViewController{  
  6.   
  7.     NSObject<ViewPassValueDelegate> *delegte ;  
  8.   
  9. }  
  10.   
  11.   
  12. - (IBAction)pressed:(id)sender;//主視圖button點擊出發的Action  
  13. @property (retain, nonatomic) IBOutlet UITextField *firstField;  
  14.   
  15.   
  16.   
  17. @end  


3,在要發送資料的視圖的事件處理方法裡聲明一個secondViewController執行個體,然後賦值給 delegate,同時執行協議的 passValue方法

 

 

[java] view plaincopy 
  1. - (IBAction)pressed:(id)sender {  
  2.       
  3.     secondViewController *secondController = [[secondViewController alloc] init];//執行個體化一個視圖2的對象  
  4.     delegte =  secondController;  
  5.     [delegte passValue:firstField.text];  
  6.       
  7.   
  8. }  


4,然後在接收資料檢視(secondViewController).h檔案實現自訂協議

 

 

[java] view plaincopy 
  1. //<2> 視圖1實現自訂協議  
  2. @interface secondViewController : UIViewController<ViewPassValueDelegate>  


5,在接收資料檢視(secondViewController).m檔案中實現協議中的passValue方法:

 

 

[java] view plaincopy 
  1. //<3>實現自訂delege的方法  
  2. -(void) passValue:(NSString *)value{  
  3.   
  4.   
  5.     secondField.text= value;  
  6.     NSLog(@"passValue method get Value : %@ secondField.text:%@",value,secondField.text);  
  7.   
  8.   
  9. }  

 

至此,大功告成,列印輸出會顯示你在第一個介面發送的資料,但是我把此資料賦值於頁面的UITextField對象,但是此對象得到的值總是null,無法在介面展示出來,我也很苦惱,什麼方法都嘗試完了,發現此接收方法總是會在ViewDidLoad方法之後執行,並且方法體內設定UITextField任意值都無法成功。

目前總結是,以上value只是對視圖1對象賦值時的一個指標引用,出了方法範圍,就無效了。所以在方法體內列印都是有資料的,出了方法體,就沒有持有的引用了。

 

轉自:http://blog.csdn.net/mad1989/article/details/7919504

【轉】使用storyboard實現頁面跳轉,簡單的資料傳遞

聯繫我們

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