The view controllers of your application coordinate between your data and what is actually displayed on screen. In a well written MVC application the view controllers will be where much of your essential app logic lives and in turn where you will spend a lot of time. UIKit supplies many built-in view controllers and view controllers of view controllers such as UITabBarController or UINavigationController. With these you can accomplish an amazing amount of tasks, but sometimes you need or want something a bit more custom! This article will discuss a few helpful techniques for developing and working with custom view controllers.
Working with Xcode can sometimes be a frustrating experience…
However, Xcode has a few times saving features that really make a difference in day to day development. In this post we are going to break from the traditional format in order to examine a few of these features.
Xcode snippets have gotten a bad wrap since they were introduced. This is mostly because they seem to lack some obvious features and are poorly documented. However, this doesn’t stop them from being an immensely powerful addition to Xcode. So let’s dive into using existing snippets and creating new ones.
- You’re familiar with UIAlertView.
- You have good understanding of the delegate pattern.
- You have at least heard of Blocks in Objective-C.
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?
This recipe will demonstrate how to use CAGradientLayer to add gradients to a custom UIView.
- You should have basic knowledge of UIView and Core Animation, specifically CALayer.
- Basic knowledge of autolayout usage in interface builder.
Setting Up A Sample Application
- Create a new Single View Application in Xcode named GradientViewDemo. Make sure to check Use Automatic Reference Counting and uncheck Use Storyboards. Our demo app will also be iPhone only to keep things simple, but go ahead make a universal app if you like (everything we will do is applicable on both iPhone and iPad),
- Create a new UIView subclass named NSCBGradientView. We added the NSCB class prefix, because this may be a valuable component to reuse in future projects and the prefix will help avoid name clashing.
- Next, we need to add QuartzCore to the target. Do so by selecting the project file in the source list on the left, then ensure the GradientViewDemo target has been selected, select the Build Phases tab from the tab bar at the top, expand the Link Binary with Libraries, and finally click the + button in the lower left corner. Search for QuartzCore, and then add it to the target.
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!
- 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.
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!
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:
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.
- You have basic knowledge of Objective-C and Foundation.
- You are familiar with NSArray and NSDictionary.
What Are Literals?
Literals in programming refer to constant values that can be directly written out in source code.
"dog food" //C string literal
@"dog food" //Obj-C string literal
17 //integer literal
17.0f //float literal
The listing above shows a few common examples of literals you probably use frequently.
- You have enough familiarity with UIKit and Xcode to create projects and UIViews. If not, consider starting with Recipe 1 and reviewing Familiarizing Yourself With Xcode
Even the slightest drop shadow here and there can dramatically improve the look of your application’s UI, but at what cost?
Everyone wants to make their app look unique! Well the UIAppearance protocol can help you! In iOS 5.0 Apple introduced the UIAppearance proxy API which allows you the developer to customize many of the appearance aspects of UIKit elements. I know what you’re thinking… Can’t I already do that? Well sure, but not on the same scale as UIAppearance will allow, and furthermore, changing the appearance of UI components on a per object basis gets really old, really fast.
So let’s dive into an example right quick…
Say you have an app with a UINavigationBar and you want something other than the default background color (tint color). You would probably try something similar to the following
Then you decide another part of your app needs a navigation bar and you end up writing the same code to change the background color for that one as well. This continues as time goes on and before long you end up with an unmanageable mess, leaving you sad and depressed wishing there was a better way… enter UIAppearance…
Creating a custom UIView using a Nib
- You are familiar with creating UIView subclasses, and instantiating UIView’s both from a Nib file or in code
- You are familiar with Nib files
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.
- 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.