1. 程式人生 > >Xcode 4.3 StoryBoard 基礎 事件和跳轉處理

Xcode 4.3 StoryBoard 基礎 事件和跳轉處理

剛學iOS 就直接用 xcode 4.3  頭瞬間大了  不過學了 之後發現比之前的xib 方便好多也滿順手  只是網路上的教程不多 。

下面是頁面進行跳轉的方法


頁面上跳轉 一般有兩種   一種是 用view 當事件源。 比如上圖 按住control 點選button 直接拉到 下個介面

 當點選 button  就直接跳轉 連線的介面。

還有種 是controller 連線 controller 。  就是 兩個介面直接拉   然後設定 中間線 segue 的identifier 按自己喜好輸

然後 在你要跳轉的程式碼段中 加

[selfperformSegueWithIdentifier:

@"gotoOther"sender:self];

下面新建個UIViewController

這是 .h 檔案

#import<UIKit/UIKit.h>

@interface firstController :UIViewController

@end

回到storyboard  點選第一個介面  設定它的 class 為剛建的firstController


接著點選 然後介面變成

你可以看到與當前介面繫結的controller 程式碼

直接按住control 從 button 拉到程式碼中  系統會自動彈出選擇筐  你可以選它為 outlet 或者是 事件


那你就可以寫程式碼拉    還可以把多個事件 繫結到同一個方法中 

- (IBAction)bt_pressed:(id)sender {

    if([bt_go isEqual:sender])

    {

        [selfperformSegueWithIdentifier:@"gotoOther"sender:self];

    }

    if([bt_cancel isEqual:sender])

    {

        exit(0);

    }

}

可以直接選擇事件進行繫結


有時候 你會發現程式碼不對    可以通過automatic 選擇正確的檔案

最後是一些跳轉和返回的方法

//根據 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];