Find articles from my Blog Archive:

Saturday, 16 October 2010

iPhone programmeing

When I first started tinkering with iPhone programming I struggled for a while trying to build what I thought was a straight-forward application. I wanted a tableView within a NavigationController withing a TabBar. This is a very common application framework, but Apple in their infinite wisdom have chosen not to make a template for it - we have tabbed application and navigation-based application, but not a combination of the two. To make matters worse, none of the books I've used seem to use this as an example, either. Being a novice I struggled for quite a while before I got this working. The other week I was asked by @perrins if I had an example as he too was stuggling to make this work. This is obviously a common issue, so I have set about writing down the steps to create just such an application below, in the hope that it will help some others.

1. Create a new “Navigation-based Application” project in XCode. In my case I have called it tabNavEg

2. Open your Application Delegate header (in my case, tabNavEgApplicationDelegate.h) and make the following changes:

a. Add UITabBarControllerDelegate as a protocol on the interface signature

b. Add tabBarController to the interface signature

c. Add a corresponding tabBarController property statement. Your Application Delegate should now look like this:


// tabNavEgAppDelegate.h

// tabNavEg


// Created by Duncan Anderson on 08/10/2010.

// Copyright __MyCompanyName__ 2010. All rights reserved.


#import <UIKit/UIKit.h>

#import <CoreData/CoreData.h>

@interface tabNavEgAppDelegate : NSObject {

UIWindow *window;

UINavigationController *navigationController;

UITabBarController *tabBarController;


NSManagedObjectContext *managedObjectContext_;

NSManagedObjectModel *managedObjectModel_;

NSPersistentStoreCoordinator *persistentStoreCoordinator_;


@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;

@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;

@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

- (NSString *)applicationDocumentsDirectory;


3. Open your Application Delegate class file (tabNavEgApplicationDelegate.m) and make the following changes:

a. Add a @synthesize tabBarController; statement

b. Replace [window addSubview:navigationController.view]; in the didFinishLaunchingWithOptions method with [window addSubview:tabBarController.view];

c. In the dealloc method add a [tabBarController release]; statement

4. Double-click MainWindow.xib to open it in Interface Builder

a. Change MainWindow.xib into list view

b. Drag a Tab Bar Controller from the library onto MainWindow.xib

c. Click on NavigationController in MainWindow.xib and rename it to FirstTabViewController

d. Drag FirstTabViewController and drop it so that it is within Tab Bar Controller

e. Click on Tab Nav Eg App Delegate in MainWindow.xib, go to the Outlets tab on the inspector, click and drag from tabBarController to connect it to the Tab Bar Controller within MainWindow.xib

No comments :

Post a Comment