您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 7 技巧

iOS 開發中的視圖切換:push/modal/popover/replace/custom 小結

編輯:iOS 7 技巧

本文來小結一下 iOS 開發中幾個應用視圖的知識點:push/modal/popover/replace/custom。自從 Storyboard 出現後,設計 View 變得更加容易。

在 storyboard 中,segue 有幾種不同類型,而 iPhone 與 iPad 開發中,segue 的類型也有所不同。iPhone 中,segue 有 push、modal 與 custom 三種,差別在於新頁面出現的方式;而 iPad 中,則有 push、modal、popover、replace 與 custom 五種。

modal 模態轉換

最常用的場景,新場景完全蓋住舊的那個,用戶無法再與上一個場景互動,除非先關閉它。它是 viewController 中標準的切換方式,可選擇切換動畫。Modal View 會彈出一個 view,你只能在該 view 上操作,對應的 segue type 就是 modal segue。

Push 類型通常需要頭一個界面是 Navigation Controller,是在 navigation View Controller 中下一級時那種從右側劃入的方式。

popover 類型則是採用浮動窗的形式把新頁面展示出來;replace 類型就是替換;custom 則是自訂跳轉方式。

視圖之間的資料傳遞

當你從當前場景觸發一個 segue 時,系統會自動呼叫 prepareForSegue:sender: 這個方法。若想從一個界面切換到另一個界面時傳遞資料,就應該重寫這個方法。

A → B,想把 NSString A_data 從 AController 傳到 BController,則在 BController 中 @property 一個 NSString data,然後在 AController 中加上:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"The segue id is %@", segue.identifier );
    UIViewController *destination = segue.destinationViewController; 
    if ([destination respondsToSelector:@selector(setData:)])
    {
        [destination setValue:@"這是要傳遞的數據" forKey:@"data"];
    }  
}

之後,BController 中的 data 屬性就接收到資料了。

ViewController 之間的跳轉

1、若 Storyboard 中當前 ViewController 與要跳轉的 ViewController 之間已存在 segue,可執行 performSegueWithIdentifier:sender: 實現跳轉。

2、若目標 ViewController 存在於 Storyboard 中但沒有 segue,可透過 UIStoryboard 的 instantiateViewControllerWithIdentifier: 取得它,再用你想要的方式跳轉,如壓棧。

3、若目標 ViewController 不存在,那就去建立它吧。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved