Last updated by at .

iOS Programming Recipe 22: Simplify UIAlertView with Blocks

Assumptions

Getting Started

Have you ever found yourself wanting to present an alertView to the user to get simple yes or no feedback? If you’re anything like me you were very annoyed that you were required to implement the UIAlertView delegate protocol just to handle this simple task.

Is There A Better Way?

[Read more...]

iOS Programming Recipe 19: Using Core Motion to Access Gyro and Accelerometer

One of the coolest things smartphones are capable of now is the ability to sense motion and orientation. The iPhone does this with the use of a 3-axis Accelerometers and Gyroscopes. In this tutorial we’re going to create an app that indicates the current G and speed of rotation. Not exactly a pretty app but still instructional.

Assumptions

  • You have a developer account and have provisioned a device. Unfortunately we can’t simulate this stuff without hardware. Follow the Apple documentation here to get set up.
  • Make Sure you head over our Github page and download the source code. NSCookbook Github Recipe 19
  • You have looked over some of the past tutorials and are comfortable with creating outlets and actions from the interface builder

Setting Up the Interface

Our Interface will be pretty simple as we will be displaying 12 values. We’ll show Max and Current Acceleration and Rotation about the X,Y and Z axis’.

Start with a new single view project titled “GyrosAndAccelerometers” and make sure the storyboards are selected.

Once the new project is created, open up the storyboard and drag 25 Labels onto the view. Twelve of these labels will be our outlets that will display the output data for each axis. Rename the labels and add a button as shown in the image below:

[Read more...]

iOS Programming Recipe 18: Unit Testing With GHUnit & CocoaPods

This Recipe will cover using GHUnit to unit test the Weather Application we developed in Recipe 15: Building A Weather Application. GHUnit is an open source unit testing framework for Mac and iOS that (in my opinion) has capabilities superior to the unit testing capabilities built into Xcode. Many developers have there own philosophy on unit testing and this Recipe will not attempt to define another. We will not be defining unit testing best practices or even go into detail about writing particular tests. This Recipe is purely about getting started with GHUnit and how it can be used in the context of a real application. Oh and did I mention? GHUnit makes writing asynchronous unit tests a breeze!

Assumptions

  • This recipe uses the Weather Application created in Recipe 15: Building A Weather Application, it is highly recommended that you work through or at least familiarize yourself with that recipe before continuing on to this one.
  • Source code for the weather app can be downloaded via GitHub. The revision of the Weather App after Recipe 15 has been tagged recipe-15, make sure to start there.
  • This recipe also relies heavily on CocoaPods, it is recommended you watch NSScreencast’s video tutorial if you are not familiar with CocoaPods.

Getting Started

Download The Starting Application
  • First download the source code for our starting application, which happens to be the Weather App developed in Recipe 15.
  • Refer to Recipe 15 for details on setting up a Weather Underground developer account in order to obtain a personal API key. You will need this key in order to access Weather Underground’s services.
  • The Weather App was built using CocoaPods, so after getting the source code you will need to navigate to the project root directory (where the .xcodeproj file lives) and run the following command in terminal. Note this will not work if you have not yet installed CocoaPods, learn about doing so here.

1
$ pod install
  • This will add all of the necessary external dependencies the Weather App needs. It will also create an .xcworkspace file which you will need to open when accessing this project (not the .xcodeproj file).
  • Add your person API key for Weather Underground to WeatherAPIKey.h, then build & run the application. If everything has been done right you should now have a functioning weather application!

[Read more...]

iOS Programming Recipe 15: Building A Weather Application

This recipe will cover building a basic weather application for the iPhone from start to finish (no graphics needed, all code). Initially, this application will be very basic, but many enhancements/improvements may be the topic of future recipes. This recipe will move fast! If you find yourself having trouble keeping up you should go back and work through our earlier recipes before attempting this one. When you’re done with this recipe, you should have an app that looks like the following:

recipe15-8

Before We Get Started

  • This application will be built using CocoaPods which is an Objective-C dependency manager. If you are not familiar with CocoaPods or just need some help getting it installed please visit their website. Additionally, there is a very good video tutorial on CocoaPods that NSScreencast has done which is currently free to view. Make sure you get CocoaPods installed on your system before starting this article, because installation will not be covered in the recipe.
  • The application will also use API provided by Weather Underground which will require you to sign up for a free developer API Key (limited to 500 requests per day I believe). Don’t worry this is very simple and will be covered in the recipe.
  • The source code to this recipe is available online through GitHub, and it may help to check it out when working through this tutorial.

[Read more...]

iOS Programming Recipe 14: Implementing A UICollectionViewController

UICollectionView is great for displaying information in a grid or other custom layouts. In iOS 6 you’ll see collection views most likely used in many apps such as the podcasts app for viewing different podcasts. Since in most cases you’ll want to use UICollectionViewController, we’ll focus on that for this recipe.

Assumptions

  • You are familiar with Xcode and setting up a single view controller. If not check out our recipe on Xcode first.

Setting Up The View

Go ahead and create a new single view controller project. I titled mine “CollectioViewFun”. When the project opens up, You’ll want to delete the single view controller from the storyboard and drag a new collection view controller in it’s place. This should look like the following:

CollectionViewController

Now since our class type has changed from a standard view controller to a collection view controller we’ll want to change the class type in the viewController.h file as well. Go ahead and change the class name from ViewController to CollectionViewController as well. Don’t forget to change all instances of the term “ViewController” in both the .m and .h files:

[Read more...]

iOS Programming Recipe 11: Using The UITableView Part I

In most apps you’re sure to find a table view whenever a list of items needs to be presented. UITableView, similar to UIPickerView, declares both a data source and delegate protocol. The data source protocol is required while the delegate is optional. We’ll need to make use of the data source protocol for the UITableView class. For this recipe I chose to show a table view with bugs and animals shown. The bugs and animals will have seperate sections.

Assumptions

  • You are familiar with Xcode, if not familiarize yourself here.

Setting Up the View

First start with a single view controller project. Now drag a UITableView object onto the existing view from the object library and then drag a UITableViewCell onto the newly created table view. When you’re done you should have something that looks like this:

[Read more...]

iOS Programming Recipe 10: Adding A Shadow To UIView

Assumptions

Shadows!

Even the slightest drop shadow here and there can dramatically improve the look of your application’s UI, but at what cost?

Today we will cover adding shadows to UIViews of all kinds. Feel free to check out the source code for this recipe available on our GitHub page.

[Read more...]

iOS Programming Recipe 6: Creating a custom UIView using a Nib

Creating a custom UIView using a Nib

Assumptions
  1. You are familiar with creating UIView subclasses, and instantiating UIView’s both from a Nib file or in code
  2. You are familiar with Nib files
Background

Sometimes you find yourself trying to create a quick composite UIView (UIView subclass w/ multiple subviews) where a UIViewController doesn’t seem necessary Please note that a UIViewController is the right choice most of the time. This can be a real pain to setup entirely in code if you have many subviews, and god forbid if you want to use auto layout! So you may find yourself wanting to use a nib to simplify things a bit, well this tutorial will go through the process of doing just that.

Getting Started
  • Create a new Xcode project based on the single view application template for iOS. This tutorial will assume you are using ARC, so you may want to make that selection when creating the new project.
  • Once you have created the new project a new UIView subclass to the project and name it CustomView.
  • Then create a new Nib file named CustomView.nib and add it to the project.
[Read more...]

iOS Programming Recipe 5: Passing Values Between Segues with PrepareForSegue

In Part 1 and 2 of Recipe 3 I stepped through creating a modal relationship using storyboards. There was no code needed to make this relationship using storyboards. If you want to pass information from the presenting view to the presented view you’ll need to add some code.

Assumptions

  • You know how to set up a segue relationship using the storyboard. If not go read part 1 and 2 of Recipe 3 here: Part 1Part 2

Setting Up the Views

To start off, I created a new single view controller project and replaced the view controller with a navigation view controller. Then I deleted the root controller and added two new view controllers. I made one view controller the root view controller, and I didn’t setup the segue on the other controller for now. Then I gave each views the titles “Root View Controller” and “First View Controller” respectively. See recipe 3 for more details on setting this up.

Next I deleted the viewController class and all of it’s refferences. Delete the viewController.m file and viewController.h file and Xcode will ask you if you would like to remove the refferences.

Next we’ll need to create two new UIViewController Classes. To do this push the plus sign in the bottom left hand corner of the project navigator.

Create New UIViewController Classes

When prompted choose objective-C class under Cocoa Touch and press next. For the next prompt name the Class “RootViewController” and choose the subclass “UIViewController” then press next. You will then press create on the next prompt. Repeat this step and name the next class “FirstViewController”.

Choosing UIViewController subclass

When you’re done creating classes you should see the newly created .m and .h files in the project navigator.

New Project Navigator

 

The last thing we’ll want to do is connect our new classes to our views on the storyboard. To do this, select the view and in the utilities pane under the identity inspector choose the class from the dropdown.  Do this for both the RootViewController and the FirstViewController.

[Read more...]

css.php
Privacy Policy