How to Customize Your iOS App Table View for the iPad
How to Set Up a WeatherController in the Main_iPad.storyboard file for Your iOS App
How to Add and Set Up the EventPageController in the MainStoryboard in Your iOS App

How to Add a New View Controller to Your iOS App

Your first step in adding a new view controller to your iOS app is to select the iPad storyboard file in the Project navigator. With the storyboard displayed, you then make sure that the Utility area is visible by clicking its icon in the Xcode toolbar’s View selector. With that done, you can now hide the Project navigator by clicking its icon in the Xcode toolbar’s View selector.

As the last step in getting your canvas ready, click the Attributes inspector button in the Inspector selector in the Utility area.

To add the Test Drive controller, you need to do the following:

1

Select Objects in the Utility area’s Library pane, and then drag a new view controller from the pane into your storyboard.

Whereas you can add controls and other views to views only when the storyboard elements are full size, you can add view controllers at any zoom level.

A new scene is created.

2

Select the Table view in the Master View Controller – Master Scene and then select the Attributes inspector.

If you look at the Canvas, you see a Table view with Prototype Cells, and a cell with the text of Title.

You’ll notice that, in the Table View section of the Attributes inspector, the Dynamic Prototypes option is selected.

Right now, if you select a cell, nothing happens. That’s because with Dynamic Prototype cells, you have to implement a method in your view controller to do something when a cell is selected.

Use the Attributes inspector to change the Master view from Dynamic Prototypes to Static Cells.

Static cells are used when you know in advance what needs to be displayed in a cell.

3

In the Attributes inspector, select Static Cells from the Content drop-down menu.

You’ll notice a change in the Table view. The heading Prototype Cells will disappear and you’ll see three cells each with the text Title.

4

In the Outline view, expand the disclosure triangle next to the table view and you’ll see a Table View section.

Expand the Table View section and you’ll see three Table View cells. Expand the first Table View cell and you’ll find a content view and then a label. Select the label, and in the Attributes inspector Title field, enter Test Drive.

5

Select the first Table View cell (the cell, not the Test Drive label) in the Document Outline, and Control-drag from the cell in the Master View controller to the view controller you just added. Then release the mouse button.

Control-clicking from a button or Table View cell and dragging to the view controller you want displayed creates a selection segue or an accessory action. When you release the mouse button, you’ll see the Storyboard Segues contextual menu, which pops up onscreen.

6

Select Push from the Selection Segue pop-up menu.

A segue performs the visual transition between two view controllers and supports push (navigation), modal, and custom transitions.

A push segue changes the scene — and the user sees the new view controller’s view (with its Back button) slide into place when the user taps a button.

In contrast to a push segue, a modal segue presents the view controller modally, with the transition style you specify, and requires the user to do something to get back to the previous view controller. As for custom transitions, segues support the standard visual transition styles such as Cover Vertical, Flip Horizontal, Cross Dissolve, and Partial Curl.

Segue objects are used to prepare for the transition from one view controller to another, which means segue objects contain information about both view controllers involved in a transition. When a segue is triggered — but before the visual transition occurs — the storyboard runtime calls the current view controller’s prepareForSegue:sender: method so that it can pass any needed data to the view controller that's about to be displayed.

You’ll notice that selecting Push from the Storyboard Segue’s pop-up menu causes the Navigation bar to appear but also shrinks the view.

7

Select the Push segue in the Master View Controller scene. After making sure that Push appears on the Style menu in the Attributes inspector, enter TestDrive in the inspector’s Identifier field; then press return (or enter).

You won’t always use the identifier, but it's good practice to name it so that you can identify the segue.

The field in the storyboard isn't updated until you press return, or sometimes until you click in another field in that inspector.

8

Choose Detail Split from the Attributes inspector’s Destination drop-down menu.

The size of the view in the Test Drive controller changes.

9

Finally, select the two unused Table View cells in the Document Outline and delete them by pressing Delete.

The default destination was set to Current, which meant that it was set to the Master view, because that’s where you were dragging from. With that default, the view controller had been resized for the Master view, which is 320 points wide in the standard Split View Controller. But you want the destination to be in the Detail view; choosing Detail Split in this step does that for you.

You can see that the view has now been sized down and that a Navigation bar has been added to the top of the view. If you expand the view controller in the View Controller Scene in the Document Outline, you can see that a Navigation bar was added there as one would expect.

You’ll also notice that a Disclosure Indicator (the chevron — a right-arrowhead-like shape on the right side of the Test Drive cell) has also been added.

When you select the Detail cell and create the Push segue with the Detail view as the destination, the new Test Drive controller becomes embedded in the Detail view’s Navigation controller. This Navigation controller manages the view controller stack for everything in the Detail view of the Split View Controller.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Add the Weather View Controller to Your iOS App
How to Use a Navigation Controller in the Master View for Your iOS App
How to Add a Background Image to Your iOS App
How to Add a Toolbar to Your iOS App
How to Use Inspector and Quick Help Pane for iOS App Development
Advertisement

Inside Dummies.com