Bang head here

So I’ve gotten pretty good with basic iPhone programming.  Or at least, good enough to pass as semi-qualified.  I’ve managed to get an app in the App Store and it’s selling about a copy a day so far – not a huge amount of income, but probably a lunch or two a month.  I’m fairly happy with that, since this is the first app I’ve done specifically for myself, from my own idea.  I’m working on another one now that I’m hoping will have a slightly broader appeal, but I’m still not expecting it to go gangbusters.  I am, however, earning a few more things about programming that I didn’t necessarily expect to – and thanks to Admiral Frijole (yes, that is a nickname), learning a lot more than I expected to about interface design.  See, he’s able to think like Apple (though he’ll tell you it’s thinking like a user of the iPhone, not like Apple), and has pointed me in a few different directions to make the interface not just better, but completely different from anything I would have thought of on my own.

The hardest part now is figuring out how to go from sketches / mockups to the code – I’ve got a few ideas, I just need to see which one(s) work.

In the meantime, I promised a practical demonstration of doing things in code as opposed to in Interface Builder (IB), so let’s look at how I built the initial tab environment for the app that’s selling now.  First, I start with the fact that I need a TabBarController in order to make the tabs at the bottom easily, so I make the header file for the AppDelegate look like this:

#import <UIKit/UIKit.h>

@interface Brewinator_Pint101AppDelegate : NSObject  {
     UIWindow *window;
     UITabBarController *tabBarController;
}

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

@end

Now that we have the basic TabBarController defined, we simply need to built it out. I’ll be straightforward and tell you now that I’ve left some of the actual code out of this sample code that have no bearing on the interface, so the code you see will get you a pretty interface but not much else. In the applicationDidFinishLaunchingWithOptions() function in the AppDelegate.m file, I added the following code:

    UINavigationController *localNavController;

    tabBarController = [[UITabBarController alloc] init];
    NSMutableArray *localControllers = [[NSMutableArray alloc] initWithCapacity:2];

    TypeViewController *typeViewController;
    typeViewController = [[TypeViewController alloc] initWithTabBar];
    localNavController = [[UINavigationController alloc] initWithRootViewController:typeViewController];
    [localControllers addObject:localNavController];
    [localNavController release];
    [typeViewController release];

    NameViewController *nameViewController;
    nameViewController = [[NameViewController alloc] initWithTabBar];
    localNavController = [[UINavigationController alloc] initWithRootViewController:nameViewController];
    [localControllers addObject:localNavController];
    [localNavController release];
    [nameViewController release];

    tabBarController.viewControllers = localControllers;

    [localControllers release];
    // Add the tab bar controller's view to the window and display.
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];

A few things to point out here. The TypeViewController and NameViewController classes are defined elsewhere; the header files for those classes need to be #imported in the AppDelegate’s .h or .m file appropriately. The initWithTabBar() routine called on each of them also needs to be defined and implemented properly inside each ViewController’s .h and .m file respectively.  If you want more than two tabs, you will need to initialize the localControllers array with the appropriate capacity and then repeat each ViewController “stanza” once per desired tab.

Once those are taken care of, you should be able to build and run the code in the simulator, and it will give you an interface with two tabs at the bottom that you can switch between.  Until you define the interface in each ViewController’s loadView() function, they won’t really look like much, but that’s a post for another day.

For now, enjoy your tabbing display!

Comments are closed.