天天看點

轉換到 StoryBoard 的釋出說明(Converting to Storyboards Release Notes)

轉換到 StoryBoard 的釋出說明(Converting to Storyboards Release Notes)

<a target="_blank" href="http://blog.csdn.net/opengl_es">轉載請保留此句:太陽火神的美麗人生 -  本部落格專注于 靈活開發及移動和物聯裝置研究:iOS、Android、Html5、Arduino、pcDuino,否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。</a>

<a target="_blank" href="https://developer.apple.com/library/ios/releasenotes/Miscellaneous/RN-AdoptingStoryboards/">轉換到 StoryBoard 的釋出說明(Converting to Storyboards Release Notes)</a>

Storyboarding 是一種新的方式用于建立 iOS 應用的使用者界面,從 iOS 5 和 Xcode 4.2 開始引入。使用 storyboard,你可以把組成你的應用的視圖控制器設計成 Xcode 設計畫布中的場景,并且使用 segue 在場景間可視化地定義導航。

Storyboarding is a new way to create user interfaces for iOS applications, beginning with iOS 5 and Xcode 4.2. Using storyboards, you can design the view controllers that compose your application as scenes in the Xcode design canvas and visually define the navigation between the scenes using segues.

把一個已有的 iOS 應用轉換成使用 storyboard 需要采取一些步驟。另外,有一些其它模式可以采用。

There are a few steps you need to take to convert an existing iOS application project to use storyboards. In addition, there are other new patterns you can adopt.

内容如下:

Contents:

配置應用代理

Configure the Application Delegate

添加 Storyboard 到工程

Add a Storyboard to the Project

為工程設定主 Storyboard

Set the Main Storyboard for the Project

通路第一個視圖控制器

Accessing the First View Controller

配置表視圖

Configuring Table Views

如果你沒有應用代理類,那麼需要建立一個。一個最小實作會像這樣:

If you don’t have an existing application delegate class, you need to create one. A minimal implementation would look like this:

Listing 1-1 最小應用代理頭檔案 

Listing 1-1 Minimal application delegate header file

#import &lt;UIKit/UIKit.h&gt;

@interface AppDelegate : NSObject &lt;UIApplicationDelegate&gt;

@property (strong, nonatomic) UIWindow *window;

@end

Listing 1-2 最小應用代理實作檔案

Listing 1-2  Minimal application delegate implementation file

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;

在 main.m 檔案中 UIApplicationMain 類裡設定應用代理。

In the main.m file, set the application delegate class in UIApplicationMain.

已有的 main.m 檔案可能看起來像是這樣:

Your existing main.m file probably looks something like this:

int main(int argc, char *argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    int retVal = UIApplicationMain(argc, argv, nil, nil);

    [pool release];

    return retVal;

}

把它改成這樣:

Change it to look like this:

    @autoreleasepool {

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

    }

(将 “AppDelegate” 替換為你的應用代理類的名字).

(replace “AppDelegate” with the name of your application delegate class).

添加 Storyboard 到工程中

<a target="_blank" href="https://developer.apple.com/library/ios/recipes/xcode_help-structure_navigator/articles/Adding_a_New_File.html#//apple_ref/doc/uid/TP40009934-CH4"></a>

從對象庫中添加第一個視圖控制器到 storyboard。你應該會看一個無源的 segue,它訓示這是第一個場景。

Add your first view controller to the storyboard from the Object library. You should see a sourceless segue indicating that this is the first scene.

如果第一個視圖控制器嵌入在諸如導航控制器或分頁控制器這樣的容器中,那麼使用 Editor &gt; Embed In 來正确地嵌入它。無源 segue 現在應該指向容器視圖控制器了。

If the first view controller is embedded in a container such as a navigation controller or tab bar controller, then use Editor &gt; Embed In to embed it appropriately. The sourceless segue should now point to the container view controller:

為項目設定主 Storyboard

In the Summary for application Target, set the value of the Main Storyboard to the name of the storyboard file you created. If there is a value for Main Interface (to specify the first nib file), make sure you remove it.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UINavigationController *rootNavigationController = (UINavigationController *)self.window.rootViewController;

    MyViewController *myViewController = (MyViewController *)[rootNavigationController topViewController];

    // Configure myViewController.

    return YES;

There are several new ways of working with table views when you use storyboards.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

// Configure and return the cell.

you would now write just:

For a table view that is the view of a UITableViewController instance, you can configure static content directly in the storyboard. In the Attributes Inspector, set the content of the table view to Static Cells. 

If you use static cells, you can connect outlets from the table view controller to individual cells so that you can configure the cell’s content at runtime.

// Declare properties for the outlets.

@property (nonatomic, weak) IBOutlet UITableViewCell *firstGroupFirstRowCell;

// Configure cells directly.

firstGroupFirstRowCell.detailTextLabel.text = newTextValue;

繼續閱讀