Converting to storyboards Release Notes)

Source: Internet
Author: User


Converting to storyboards Release Notes)

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 ideally define the navigation between the scenes using segues.

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
  • Add a storyboard to the Project
  • Set the main storyboard for the project
  • Accessing the first View Controller
  • Grouping table views

Configure the application delegate

The application delegate is responsible for loading the storyboard and managing the window. You need to specify the name of the application delegate class in uiapplicationmain, and ensure that the Application delegate has a property called window.

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

Listing 1-1Minimal application delegate header file

# Import <uikit/uikit. h>

@ Interface appdelegate: nsobject <uiapplicationdelegate>

@ Property (strong, nonatomic) uiwindow * window;

@ End

Listing 1-2Minimal application delegate implementation file

# Import "appdelegate. H"

@ Implementation appdelegate

@ Synthesize window = _ window;

@ End

Note:In the current xcode templates, the application delegate class inherits from uiresponder. this is so that the delegate instance can participate in the responder chain and so handle application-level actions. if you haven't made use of this pattern in an existing application, there's no need to adopt it for storyboards.

In the main. M file, set the application delegate class in uiapplicationmain.

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

# Import <uikit/uikit. h>

 

Int main (INT argc, char * argv []) {

 

NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];

Int retval = uiapplicationmain (argc, argv, nil, nil );

[Pool release];

Return retval;

}

Change it to look like this:

# Import <uikit/uikit. h>

# Import "appdelegate. H"

 

Int main (INT argc, char * argv []) {

 

@ Autoreleasepool {

Return uiapplicationmain (argc, argv, nil, nsstringfromclass ([appdelegate class]);

}

}

(Replace "appdelegate" with the name of your application delegate class ).

Note:@ Autoreleasepool is a new objective-C statement for managing autorelsponpools; it is available in all objective-C modes, and is more efficient than using the NSAID utoreleasepool class (see transitioning to arc release notes ).

Add a storyboard to the Project

Add a new storyboard file to the project. By convention, the initial storyboard is named mainstoryboard.

Add your first View Controller to the storyboard from the object library. You shoshould see a sourceless segue indicating that this is the first scene.


If the first view controller is embedded in a container such as a navigation controller or tab bar controller, then use editor> embed in to embed it appropriately. the sourceless segue shocould now point to the container View Controller:


Set the main storyboard for the project

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.


Accessing the first View Controller

The application delegate is not represented in the storyboard. if you need to access the first View Controller (for example, if you are creating a core data application and want to pass the delegate's managed object context to the first View Controller ), you can do so via the window's rootviewcontroller. if the Root View Controller is a container controller-such as an instance of uinavigationcontroller-then you can access your view controller using the appropriate accessor for the container's contents, for example:

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {

 

Uinavigationcontroller * rootnavigationcontroller = (uinavigationcontroller *) self. Window. rootviewcontroller;

Myviewcontroller * myviewcontroller = (myviewcontroller *) [rootnavigationcontroller topviewcontroller];

// Configure myviewcontroller.

Return yes;

}

Grouping table views

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

  • The dequeuereusablecellwithidentifier: method is guaranteed to return a cell (provided that you have defined a cell with the given identifier ). thus there is no need to use the "check the return value of the method" pattern as was the case in the previous typical Implementation of tableview: cellforrowatindexpath :. instead:
  • Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellidentifier];
  • If (cell = nil ){
  • Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier];
  • }
  • // Configure and return the cell.


  • You wocould now write just:
  • Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellidentifier];
  • // Configure and return the cell.

  • You can configure table View cells directly in the table view. by default, the prototype cell style is set to custom, so that you can design your own cell. you can also set the style to one of the built-in uitableviewcell cell styles by using the attributes inspector.
  • 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;



Copyright©2014 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | updated: 2011-10-12





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.