Use your business data to your advantage with the help of Syncfusion’s new data science offerings. Discover how a custom big data solution can provide your company with valuable predictions about key market trends.

Configuring storyboards for a project

Getting ready

In this recipe, we will learn how to configure an application's project properties using Xcode so that it is set up correctly to use a storyboard file.

How to do it...

To begin, perform the following simple steps:

Select your project from the project navigator window.

Then, select your project target from under the TARGETS group and select the Summary tab.

Select MainStoryboard from the Main Storyboard drop-down menu, as shown in the preceding screenshot.

How it works...

In this recipe, we gained an understanding of what storyboards are, as well as how they differ from user interfaces created in the past, whereby a new view would need to be created for each XIB file for your application.

Whether you are creating applications for the iPad or iPhone, each view controller that gets created within your storyboard represents the contents of a single screen, comprised of the contents of more than one scene.

Each object contained within a view controller can be linked to another view controller that implements another scene.

In our final steps, we looked at how to configure our project properties so that it is set up to use the storyboard user interface file by our application.

There's more…

You can also choose to manually add new Storyboard template to your project. This can be achieved by performing the following simple steps:

Select your project from the project navigator window.

Select File New | File…| or press command + N.

Select the Storyboard template from the list of available templates, located under the User Interface subsection within the iOS section.

Click on the Next button to proceed to the next step in the wizard.

Ensure that you have selected iPhone from under the Device Family drop-down menu.

Click on the Next button to proceed to the next step in the wizard.

Specify the name of the storyboard file within the Save As field as the name of the file to be created.

Click on the Create button to save the file to the specified folder.

Finally, when we create our project using storyboards, we will need to modify our application's delegate AppDelegate.m file, as shown in the following code snippet:

Creating a Twitter application

In this recipe, we will learn how to create a single view application to build our Twitter application.

Getting ready

In this recipe, we will start by creating our TwitterExample project.

How to do it...

To begin with creating a new Xcode project, perform the following simple steps:

Launch Xcode from the /Developer/Applications folder.

Select Create a new Xcode project, or click on File | New | Project….

Select Single View Application from the list of available templates.

Click on the Next button to proceed to the next step in the wizard.

Next, enter in TwitterExample as the name of your project.

Select iPhone from under the Devices drop-down menu.

Ensure that the Use Storyboards checkbox has been checked.

Ensure that the Use Automatic Reference Counting checkbox has been checked.

Ensure that the Include Unit Tests checkbox has not been checked.

Click on the Next button to proceed to the next step in the wizard.

Specify the location where you would like to save your project.

Then, click on the Create button to save your project at the specified location.

The Company Identifier for your app needs to be unique. Apple recommends that you use the reverse domain style (for example, com.domainName.appName).

Once your project has been created, you will be presented with the Xcode development environment, along with the project files that the template created for you.

How it works...

In this recipe, we just created an application that contains a storyboard and consists of one view controller, which does not provide any functionality at the moment. In the following recipes, we will look at how we can add functionality to view controllers, create storyboard scenes, and transition between them.

Creating storyboard scenes

The process of creating scenes involves adding a new view controller to the storyboard, where each view controller is responsible for managing a single scene. A better way to describe scenes would be to think of a movie reel, where each frame that is being displayed is the actual scene that connects onto the next part.

Getting ready

When adding scenes to your storyboard file, you can add controls and views to the view controller's view, just as you would do for an XIB file, and have the ability to configure outlets and actions between your view controllers and its views.

How to do it...

To add a new scene into your storyboard file, perform the following simple steps:

From the project navigator, select the file named MainStoryboard.storyboard.

From Object Library, select and drag a new View Controller object on to the storyboard canvas. This is shown in the following screenshot:

Next, drag a Label control on to the view and change the label's text property to read About Twitter App.

Next, drag a Round Rect Button control on to the view that we will use in a later section to call the calling view. In the button's attributes, change the text to read Go Back.

Next, on the first view controller, drag a Round Rect Button control on to the view. In the button's attributes, change the text to read About Twitter App. This will be used to call the new view that we added in the previous step.

Next, on the first view controller, drag a Round Rect Button control on to the view, underneath the About Twitter App button that we created in the previous step. In the button's attributes, change the text to read Compose Tweet.

Now that we have created our scene, buttons, and actions, our next step is to configure the scene, which is shown in the next recipe.

How it works...

In this recipe, we looked at how we can add a new view controller to our storyboard and then started to add controls to each of our view controllers and customize their properties.

Next, we looked at how we can create an Action event for our Compose Tweet button that will be responsible for responding and executing the associated code behind it to display our tweet sheet.

Instead of us hooking up an event handler to the TouchUpInside event of the button, we decided to simply add an action to it and handle the output of this ourselves. These types of actions are called "instance methods". Here we are basically creating the Action method that will be responsible for allowing the user to compose and send a Twitter message.

Configuring storyboard scenes

When you want to transition from one view controller to another, you can hold down the control key and click a button, table view cell, or any other object from one view controller, and then drag it to the new view controller for a different scene. This technique of dragging between view controllers creates, what is known as a segue.

A segue is a configurable object that supports all of the same types of transitions made available to you within the UIKit class reference, such as modal transitions and navigation transitions.

Getting ready

You also have the ability to define custom transitions that replace one view controller with another.

How to do it…

In order to see how this can be achieved, we need to modify our TwitterExample application that we created in a previous recipe. Perform the following steps to do so:

Select the About Twitter App (Round Rect Button) control, and then hold down the control key while dragging it to the view controller containing the Go Back button.

Release the mouse button, and then choose the modal option from the Action Segue pop-up menu.

You will notice that a gray arrow connects both of your view controllers. When the About Twitter App button is pressed, it will display the page containing the Go Back button.

Next, we need to do the same for our second view, so that when the Go Back button is pressed, it will return back to our first view.

Repeat steps 1 to 2, but substitute the Go Back button for the About Twitter App button.

Explanations of the storyboard segues that come part of Xcode 4 are included in the following table:

Segue name

Description

modal

A modal view controller is not a specific subclass of the UIViewController class, as any type of view controller can be presented modally by your application. However, like the tab bar and navigation view controllers, you can present your view controllers modally when you want to convey a specific meaning about the relationship between the previous view hierarchy and the newly presented view hierarchy.

push

The push segues allow you to push a new view controller onto the navigation stack, just as if you were stacking plates. The view at the top of the stack being the one that gets rendered.

These allow you to customize and call a view controller from code using the prepareForSegue method, and are what you use to present the content of your application.

custom

The job of the view controller is to manage the presentation of some content and coordinate the update and the synchronization of that content with the application's underlying data objects.

In the case of a custom view controller, this involves creating a view to present the content and implementing the infrastructure needed to synchronize the contents of that view with your application's data structures.

Once you have done this, your view controllers should look like something shown in the following screenshot. You can apply a number of transitions to each of your view controllers, so that they can perform animation when they get displayed to the view.

To learn how to apply transitions to your view controller, please refer to the Applying transitions to storyboards recipe on Transitions in this article.

Now that you have applied each of the segues to both view controllers, our final step is to compile, build, and run our application.

From the Product menu, select Run. Alternatively, you can press the command + R keys to compile, build, and run the application.

The following screenshot shows our application running within the iOS Simulator, with each of their associated screens displayed:

How it works...

So there you have it. In this recipe, we have learned how to create and add new scenes into our main storyboard, as well as the process on how we are able to link these up and configure each scene when a button has been pressed through the use of segues.

There is also another way in which we can transition to scenes within our storyboard through a programmatic approach. We will be taking a closer look into this, when we embark on the Presenting storyboard view controllers programmatically recipe in this article.

Applying transitions to storyboards

In this recipe, we will look at how we can apply transition animations to views as well as each of the available transitions that come with storyboards.

Getting ready

You may have seen such transitions in applications, such as the photos app that comes as part of the iPhone and iPad, where you can apply a transition and start a slideshow.

How to do it…

In order to configure a segue to specify a kind of transition to use between the different scenes, perform the following simple steps:

Click on the segue link for the first view controller as indicated by the large circle in the following screenshot.

Click on Attributes Inspector and then click on the Transitions drop-down menu.

You have the ability to choose from the various transition types that are only applicable to the Modal style; these are explained within the following table:

Transition name

Description

Default

When this transition is selected, it uses the Cover Vertical transition style.

Cover Vertical

When the view controller is presented, its view slides up from the bottom of the screen. When the view is dismissed, it slides back down.

Flip Horizontal

When the view controller is presented, the current view initiates a horizontal 3D flip from right to left-hand side, resulting in the revealing of the new view as if it were on the back of the previous view. When this view is dismissed, the flip occurs from left to right-hand side, returning to the original view.

Cross Dissolve

When the view controller is presented, the current view fades out while the new view fades in at the same time. When the view is dismissed, a similar type of cross-fade is used to return to the original view.

Partial Curl

When the view controller is presented, one corner of the current view curls up to reveal the modal view underneath. When the view is dismissed, the curled up page uncurls itself back on top of the modal view. A modal view presented using this transition is itself prevented from presenting any additional modal views.

This transition style is supported only if the parent view controller is presenting a full-screen view and you use the UIModalPresentationFullScreen modal presentation style. Attempting to use a different form factor for the parent view or a different presentation style triggers an exception.

These transitions come as part of the UIViewController class that inherits from the UIKit framework. The UIViewController class provides the underlying fundamental viewmanagement model for all of your iOS apps.

How it works...

Xcode provides the option of changing the visual appearance of the transition as it takes place from one scene to another within a storyboard. These are commonly referred to as segues.

Using transitions enables you to apply a variety of different styles to each of your view controllers that are to be rendered and displayed to the view, and are represented by arrows between each of the view controllers. By default, a Cover Vertical transition is performed whereby the new scene slides vertically upwards from the bottom of the view to cover the currently displayed scene.

You have the ability to define custom transitions that enable you to provide a custom segue class method to handle the transition. This can be achieved by selecting custom for the style of a segue, and fill in the name of the custom segue class to use. If you want to use any of the standard segue classes, you can find these located within the UIKit class.

Composing a tweet

In this recipe, we will look at how we can use the Twitter APIs for iOS 5 and interact with them effectively.

Getting ready

Twitter has provided us with some very simple APIs to follow, making it a snap to interact with them. In this recipe, we will look at how we can use these to post a tweet using the Twitter composition sheet.

How to do it…

In order to use Twitter within your application, we need to add the Twitter framework to your project. This can be achieved by performing the following simple steps:

Select your project from within the project navigator.

Then, select your project target from under the TARGETS group.

Select the Build Phases tab.

Expand the Link Binary with Libraries disclosure triangle.

Click on the + button and select Twitter.framework from the list.

Finally, click on the Add button to add the framework to your project.

Our next step is to create the code functionality that will be responsible for posting our tweet to Twitter:

Open the ViewController.m implementation file from the project navigator.

Type in the following import statements:

#import <Twitter/Twitter.h>

Next, modify the composeTweet method, as shown in the following code snippet:

How it works...

Starting with iOS 5, whenever you want to compose a Twitter message for submission, you will need to use the TWTweetComposeViewController class instance. The TWTweetComposeViewController class handles everything, and presents us with a tweet composition sheet, so that we can begin to type in our tweet.

The TWTweetComposeViewController class also enables you to set the initial Twitter text information to use, including how to go about adding images and URLs. Next, we declared a myTwitter variable to point to an instance of our TWTweetComposeViewController class.

We then used the canSendTweet class method of the TWTweetComposeViewController class to check to see if the user has correctly installed and set up Twitter. If this has not been done, this statement will fail, and a value of NO (or FALSE) will be returned to the isSuccess variable.

Next, we assigned some text to appear on our composition sheet, by setting the setInitialText method, and then displayed this to the view. Finally, we set up a handler, using the completionHandler method to notify us when the user has completed composing the tweet and display the relevant alert message based on the outcome returned by the method.

There's more…

With the release of iOS 6, integration of social networks is performed through the use of the UIActivityViewController class, or the classes that are contained with the new social framework of the iOS 6 SDK. For general-purpose social networking integration, the UIActivityViewController class is the recommended path.

When using this class, the user is presented with a screen providing a choice of social network services. Once the application has selected a target service, the class then presents the user with a blank message preview panel where the message can be reviewed and then posted.

Next, we will take a look at both the UIActivityViewController and SLComposeViewController classes to see each of their differences.

Using the UIActivityViewController class

The UIActivityViewController class is instantiated from within an application at the point when posting is ready to be made to a social network. The following code snippet shows how to use the UIActivityViewController class to handle posts to Twitter:

The preceding code snippet instantiates a new UIActivityViewController instance, and passes the text to be included within the dialog box when it is presented to the user. There is an option to include an image with the post, which we will be taking a look at a bit later.

Once the user decides to take an action to post an update from within an application, the following screen is displayed:

Once a destination social network has been selected from the list of choices, a preview sheet will be displayed. In the event that the user has not yet configured an account for the selected social network in the Settings application, a dialog box will appear providing the user to either set up an account or simply cancel the posting.

Using the SLComposeViewController class

In order to use the SLComposeViewController class, a number of steps are required to be performed. First, we need to verify whether the message can be sent to the specific social network service to ensure that the device has been properly configured to use the particular service.

This is achieved by passing through the type of service as an argument to the isAvailableForserviceType method, as shown in the following code snippet:

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeT
witter]) {
// Device has been set up to use Twitter
}

The isAvailableForServiceType method accepts the following parameters:

SLServiceTypeFacebook

SLServiceTypeTwitter

SLServiceTypeSinaWeibo

The next step is to create an instance of the SLComposeViewController class and supply an optional completion handler to be called when the compose sheet is either cancelled by the user or used to send a message. Finally, when the message is ready to be presented to the user, the SLComposeViewController object is presented modally by calling the presentViewController method of the parent view controller.

The following code snippet shows how to configure and present the SLComposeViewController class instance for posting to Twitter:

The preceding code snippet presents you with the Twitter compose sheet with the default text preconfigured using the method calls. The completion handler can be set up to pass a value back, indicating the action taken by the user within the compose sheet view. These values are explained in the following table:

Compose sheet values

Description

SLComposeViewControllerResultCancelled

The user cancelled the composition session by touching the Cancel button.

SLComposeViewControllerResultDone

The user sent the composed message by touching the Sent button.

The social framework class that comes with iOS 6 contains two classes that have been designed to provide more flexibility around social networking integration.

The SLComposeViewController class, unlike the UIActivityViewController class, allows you to post a message to a specific social network service within the application code, without requiring the user to make a selection from the list of available services.

The user is then presented with a preview sheet appropriate to the specific service the application has been set up for. Using the social framework within your application is very simple, and can be achieved by performing the following simple steps:

Add Social.framework into your project similarly as we did with Twitter.framework.

Open the ViewController.m implementation file from the project navigator.

Type in the following import statement:

#import <Social/Social.h>

Next, modify the composeTweet method, as shown in the following code snippet:

In this code snippet, we declare a myTwitter variable to an instance of our TWTweetComposeViewController class. We then use the addImageinstance method, to add an image to the tweet, and then present the view along with the image to the user. Whenever you want to add images to a Twitter message for submission, you will need to use the TWTweetComposeViewController class instance. This class handles everything required, and presents us with a tweet composition sheet, so that we can add images and URLs.

Now that we have added in the final piece of code to our TwitterExample application, first we need to configure our Twitter account information prior to building and running the application. Perform the following steps to set up and configure Twitter:

Open the Settings application from the iOS home screen.

Select the Twitter option from the Settings menu.

Enter your username and password credentials, and click on the Sign In button. If you do not have a Twitter account, you can create one from this screen by selecting the Create New Account option.

Next, build and run the application by selecting Product Run| from the Product menu, or alternatively by pressing command + R.

When the compilation completes, the iOS Simulator will appear, showing our Twitter application.

When you start composing your tweet, you can choose to have your current geographical location added to your message. This feature basically uses the Google Maps API to send your location along with the tweet, and then allowing others to view the precise location on Google Maps.

Attachments can also be added to the composed message, and this can be any valid image (PNG, JPG, and so on).

Clicking on the Send button will submit the message to your Twitter account, and you will receive a message stating that the tweet has been successfully posted.

How it works...

In this recipe, we looked at how we can integrate Twitter-like functionality into our applications. There are many ways in which applications can be more social, by including Twitter. For example, you could make the application auto-tweet, when you unlock a special item within your game, or when you finish the game, or just want to upload your high-score achievements.

This lets all of their friends know they are playing your game, which in turn, gets you more exposure. Another example could be a business application, which could allow the user to tweet the number of successful projects that they have completed. With Twitter getting so much attention lately, you would be crazy to not include some sort of Twitter integration into your own iOS applications.

There's more...

Adding an image to a tweet using the SLComposeViewController class can be achieved by adopting the following code snippet:

In the preceding code snippet, we used the SLComposeViewController method to configure and present the tweet composition sheet. We then configured our composer view using the method calls for displaying default text and setting the default image.

Summary

In this article, we learned about storyboards and the different features of story board along with it. We also learned about Tweet applications.

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.