TwitterCounter for @bigclick_dean

dBlog.com.au

My Development Blog

In this installment we will be creating the “framework” for our application, first up we will be creating our UITabBarController and then we will add some ViewControllers to represent our different views.

1. Create a “Window-Based” application template

I had originally intended to create the application based on a “Navigation-Based” application template but now I have decided to create everything from scratch and not use Interface Builder. So if you have already completed part 2 then I suggest you go back there and follow the instructions to create the new “Window-Based” application template.

2. Setting up the UITabBarController and its delegate

Open up RSSReaderAppDelegate.h and edit the code to look like below


#import <UIKit/UIKit.h>

@interface RSSReaderAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}

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

@end

What we have done here is added the UITabBarControllerDelegate and created a UITabBarController instance called tabBarController.

Now you will need to open up RSSReaderAppDelegate.m and synthesize the tabBarController instance and alter the applicationDidFinishLaunching function to look like this


- (void)applicationDidFinishLaunching:(UIApplication *)application {

tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];

// Override point for customization after application launch
[window makeKeyAndVisible];
}

This will allocate and init the UITabBarController and then add it to the window (we haven’t added any buttons yet, but we will do this shortly).

If you run your application now you should see the UITabBar at the bottom of the window like so:
UITabBar with no buttons

3. Creating our UINavigationController

Right Click on the Classes folder in the left hand column and click Add > New File… , the following window will appear
XCode: Add new File

Now choose UIViewController subclass and click next, now name the class MainViewController.m and click Finish.

You should now have a MainViewController.h and a MainViewController.m file in your Classes folder. Open up MainViewController.h and edit its contents to below:


#import <UIKit/UIKit.h>

@interface MainViewController : UINavigationController {
UINavigationController *navigationController;
}

@property (nonatomic, retain) UINavigationController *navigationController;

@end

Notice we have changed the subclass from UIViewController to UINavigationController and we have added a UINavigationController instance.

Now open up MainViewController.m and edit its contents to below:


#import "MainViewController.h"

@implementation MainViewController

@synthesize navigationController;

- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"Main View Did Load: %@", self.tabBarItem.title);
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}

- (void)dealloc {
[super dealloc];
}

@end

All we have done here is synthesized the navigationController instance and put a NSLog call to display when the controller is created and what its TabBar item title was when it was created.

If you run the application now you wont see anything different, this is because we haven't added anything to the UITabBarController yet. To add the buttons you will need to open up the RSSReaderAppDelegate.m file and edit the applicationDidFinishLaunching function to look like below:


- (void)applicationDidFinishLaunching:(UIApplication *)application {

tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

// Create instances of the MainViewController for the 4 TabBar buttons
MainViewController *viewController1 = [[[MainViewController alloc] initWithNibName:nil bundle:nil] autorelease];
viewController1.tabBarItem.title = @"Browse All";
MainViewController *viewController2 = [[[MainViewController alloc] initWithNibName:nil bundle:nil] autorelease];
viewController2.tabBarItem.title = @"Most Recent";
MainViewController *viewController3 = [[[MainViewController alloc] initWithNibName:nil bundle:nil] autorelease];
viewController3.tabBarItem.title = @"Favourites";
MainViewController *viewController4 = [[[MainViewController alloc] initWithNibName:nil bundle:nil] autorelease];
viewController4.tabBarItem.title = @"Settings";
tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, viewController4, nil];

// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];

// Override point for customization after application launch
[window makeKeyAndVisible];
}

What we are doing here is creating 4 instances of the MainViewController and setting the tabBarItem title (this displays below TabBar icon). Once we have created all four of the View Controllers we add them to the tabBarController by setting its viewControllers property to an array containing the pointers for our 4 View Controllers. Remember that you need to have a nil value at the end of the array to indicate the last element.

If you run your application now you should see 4 TabBar items with the titles we have assigned them above. You will also notice that we have a Navigation bar at the top of our window, as we don't have a UITableView setup yet the Navigation bar will just be blank.

iPhone SDK: RSS Reader - TabBar & NavigationController working

4. Setting up our UITableViews

Now for starters we are going to need 4 UITableViewControllers for our Browse, Recent, Favourites and Settings Tabs. We will start with the stock standard ones and customize them as we go along.

Right Click the Classes folder in the left hand column and click Add > New File...
Select UITableViewController subclass and click Next
Call the first one BrowseViewController.m and then click Finish
Repeat these steps and create a RecentViewController, FavouritesViewController and SettingsViewController

Now that we have all our stock standard UITableViewControllers created we have to connect them up, we will be doing this in the MainViewController.m file, so open it up and edit its viewDidLoad function to below:


- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"Main View Did Load: %@", self.tabBarItem.title);
if(self.tabBarItem.title == @"Browse All") {
BrowseViewController *browseViewController = [[BrowseViewController alloc] init];
[self pushViewController:browseViewController animated:YES];
[browseViewController release];
} else if (self.tabBarItem.title == @"Most Recent") {
RecentViewController *recentViewController = [[RecentViewController alloc] init];
[self pushViewController:recentViewController animated:YES];
[recentViewController release];
} else if (self.tabBarItem.title == @"Favourites") {
FavouritesViewController *favouritesViewController = [[FavouritesViewController alloc] init];
[self pushViewController:favouritesViewController animated:YES];
[favouritesViewController release];
} else if (self.tabBarItem.title == @"Settings") {
SettingsViewController *settingsViewController = [[SettingsViewController alloc] init];
[self pushViewController:settingsViewController animated:YES];
[settingsViewController release];
}

}

Now this may not be the most traditional method and isn't very good for massive applications that use heaps of view controllers, etc but it worked fine for my needs and meant I only needed one UINavigationController (MainViewController). What it does is it checks the title of the TabBar item that it is linked to, it then decides which UITableViewController to push into view.

Also don't forget to #import the four new header files


#import "BrowseViewController.h"
#import "RecentViewController.h"
#import "FavouritesViewController.h"
#import "SettingsViewController.h"

If you run your application now you will wee that we have a UITableView visible on each of the TabBar items, but we currently cant tell which one is which as they all look the same.

5. Identifying and Customising our UITableViews

Starting with our BrowseViewController we want to open up the BrowseViewController.m file and alter it's viewDidLoad function to below:


- (void)viewDidLoad {
[super viewDidLoad];

self.title = @"Browse All";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
}

We have set the title to "Browse All" and we have added a button to the right hand side of the navigation bar, this button is an add button (as indicated by UIBarButtonSystemItemAdd) and currently its action is set to nil, in the future we will connect this up to an UIActionSheet.

Now go through the other three controllers (RecentViewController, FavouritesViewController and SettingsController) and just set the title, not the rightBarButtonItem!

If you run your application now you should be able to navigate through it and see the title change and on the Browse All tab you should see an add button at the top right, although it wont do anything when you press it.

Browse All TabMost Recent TabFavourites TabSettings Tab

6. Download the Project Files

Click Here to download my version of the Project files, you can use these to work your way through any problems or even to work backwards to see how I do things.

7. Whats Next?

In the next installment we will be creating our SQLite database and adding some feeds and groups, we will also be setting up the BrowseViewController to handle multiple levels of groups with only one view (Drilldown) and we will also be looking at storing images in a SQLite database with a BLOB field.

PLEASE CLICK HERE TO RE-TWEET THIS TUTORIAL

Please do anything you can to spread these tutorials around as the more people that read the faster I will write tutorials!

Popularity: 15% [?]

Comments

There are 36 comments for this post.

  1. Jack on April 6, 2009 1:13 am

    awesome job! Out of curiosity, I am a new “app developer” if you will, and am having a rough time learning how to write programmes. Do you have any tips that could help me get started? (other than following your great tutorial of course)any help is much appreciated! Can’t wait for the next one!

    Cheers,
    Jack

  2. Dean on April 6, 2009 2:01 am

    Hi Jack,

    The best way to learn is to just have a play around. Get an idea (preferably simple) and just run with it, try the different methods, read as many tutorials and books as you can.

    Hopefully the next few installments will help you alot.

    Cheers,
    Dean

  3. Joe on April 6, 2009 4:00 pm

    Dean,

    I am curious why you do not create instance variable of the classes you are using for UITabBarController, but rather use temporary variables and assigning them into the array. Doesn’t such practice create memory leak?

    I am still quite confused on the autorelease and memory assignment, not sure which is the correct practice to avoid headaches down the road.

  4. Dan on April 8, 2009 4:32 pm

    Great tutorial so far. Would you generally say its better to not use interface builder and construct your UI via code? I’ve been wondering this myself lately as interface builder has been pretty buggy. Thanks again for the great tutorial.

  5. WallStreetVH on April 14, 2009 10:15 pm

    I downloaded the code and it compiles and runs fine but I am only getting the original screen with no tab titles etc.

    http://dblog.com.au/wp-content/uploads/2009/04/picture-36.png

  6. greg on April 14, 2009 11:11 pm

    These are very helpful and just what I am looking to learn. Are you doing more or can we dl the code to the finished project?

  7. Janise on April 15, 2009 12:15 pm

    Great tutorial, so far.
    When is the release date for the next installment?

    Thanks.

  8. Dean on April 16, 2009 12:00 am

    Hi All!

    @Joe: In this case there is no real need to release them as when the TabBarController is destroyed (when the application is closed) they will be destroyed. This is not the case in the UINavigationController as we are creating/removing views everytime we will be drilling down. You could easily make them into instance variables and release them in the dealloc but it would make no difference.

    @greg: I am doing more parts all the way through to App Store Publication, I was hoping to get one out last weekend but I ended up having a break for Easter. This weekend should see another release.

    @Janise: Thanks for the kind words! As above I will be releasing another installment this weekend hopefully (time permitting).

    Cheers,
    Dean

  9. WallStreetVH on April 16, 2009 4:17 am

    Hi Dean,

    Any idea on this one below, could it be an SDK version issue, I use 2.0 and 2.1 and haven’t had any issues before.

    WallStreetVH Says:
    April 14th, 2009 at 10:15 pm
    I downloaded the code and it compiles and runs fine but I am only getting the original screen with no tab titles etc.

    http://dblog.com.au/wp-content/uploads/2009/04/picture-36.png

    Thanks,
    John

  10. Ashwani on April 17, 2009 11:04 am

    Hello Dean:
    Thanks for this article
    I am new to iphone application development.
    I have a navigation based application in which I want to add tab bar.
    So I followed your code but I am facing issues.
    In your code, you started from window application while I started from Navigation based template. When I am building the code tab bar is coming but navigation bar is not coming. And in my first view I have drill down table which is also not drilling . Please help in this

    Thanks is advance :)

  11. Ashwani on April 17, 2009 11:25 am

    I found the problem, anyways thanks for this code :)

  12. Mark on April 19, 2009 2:31 pm

    Was wondering what the benefit of using code to setup the TabBar and NavBar is?

    I am looking forward to your next tutorial on saving the XML feed to SQLite, as I need this for an idea I have in mind. I’d like to know whether the database will be updated automatically when the feed is updated, or how will it work?

  13. DAN on April 27, 2009 3:46 pm

    Hiya,

    Fantastic tutorial, thanks! Really looking forward to the next part… learning more lpaying with this than the rest of the tutorials put together.

    Roll on the next installment!

    Cheers,

    Dan

  14. Ryan on May 5, 2009 6:52 pm

    Hey,

    This tutorial really looks like it will be great. Many other tutorials build only simplistic applications, good for learning basics, but not great for seeing how to put together a realistically sized application. Finally a feature rich tutorial. Keep up the good work, can’t wait for more!

    Ryan

  15. Janise on May 9, 2009 2:04 pm

    Any idea when you will be able to post the next installment?

    Thanks.

  16. coderguy on May 11, 2009 11:13 pm

    Hi!

    Great, very great tutorial, clarified a lot my thoughts.

    Please, post the part 5, can’t wait for it!

  17. Craig on May 12, 2009 11:22 am

    It’s been a month, any idea when the next tutorial will be up? I’m waiting anxiously!

  18. Justin Kent on May 22, 2009 7:09 pm

    Nice work! Looking forward to the next installment.

    Another example along these lines: https://devforums.apple.com/message/14050#14050

    (you must be a paid iPhone developer to access the link)

  19. Rob on May 28, 2009 12:20 am

    Nice tutorial, thanks for the effort.

    Could you explain a few things further?

    a) Why not use Interface builder?

    I had problems with it (drag & drop), but have overcome them and have a full Tab/Nav/Table hierarchical app all structured and configured by IB. My problems were trying to force IB to build the hierarchy a certain way that it wouldn’t allow. I learned how to do it the “IB” way, and it works perfectly.

    b) Why sub-class UINavigationController?

    You have a nested UINavigationController property that doesn’t get used, and the code you use to load the root view controllers could/should be done in the Application delegate where it can simply be done with something like:

    BrowseViewController *browseViewController = [[BrowseViewController alloc] init];
    UINavigationController *viewController1 = [[UINavigationController alloc] initWithRootViewController:browseViewController];
    [browserViewController release];

    In future steps you might be using the nested UINavigationController property, and I’m missing the point.. but its not clear in these steps as to why the property is used. If the property is not used, and the initialisation code is bumped up to the app delegate, there is no need to sub-class the UINavigationController!

    Cheers,

    Rob

  20. Jonathan on June 1, 2009 3:03 am

    NSString *viewControllerName = [NSString stringWithFormat:@"%@ViewController", self.tabBarItem.title];
    UIViewController *viewController = [[NSClassFromString(viewControllerName) alloc] init];
    viewController.navigationItem.title = self.tabBarItem.title;
    [self pushViewController:viewController animated:YES];
    [viewController release];
    Hehe!

  21. Azad on June 12, 2009 5:20 am

    Excellent tutorial… waiting for the next set.. mainly on the drill down part.. when are u planing to post it?

  22. newbyiphone on June 24, 2009 7:54 pm

    This is a great series of tutorial, I can’t wait to read on the SQLite part next which I am having trouble with right now.

  23. Pradeep Sundriyal on July 1, 2009 7:16 am

    Nice tutorial…i m thankful to you

  24. Christopher on July 3, 2009 7:32 pm

    I am waiting eagerly for the next tutorial. Please post it soon!

  25. Web on July 7, 2009 3:11 am

    Hi Dean,

    This was brilliant, got me right on track.

    Any chance you are going to post your tutorial for the next step of adding a Drilldown and SQlite database?

    Many thanks,

    Web

  26. Raj on July 21, 2009 10:00 pm

    Where the URL provided for this. I meant , it will show you the feeds of some company web site. Where did u provided that information?

  27. Raj on July 21, 2009 10:35 pm

    Where is feed? This is just a exampto view the various UI controls etc. I guess we talk about the feed
    program? where is that , in the next tutorial?

  28. Mike on August 5, 2009 8:35 pm

    BUMP

    Would love to see the next installment on this series.

    It has been great so far!! Keep up the great work.

  29. Matthias on August 7, 2009 8:52 am

    Hello

    I downloaded the code and it compiles and runs fine but I am only getting the original screen with no tab titles etc.

    Please help me to understand this problem

    http://dblog.com.au/wp-content/uploads/2009/04/picture-36.png

  30. Vish on August 12, 2009 9:03 pm

    Thank you very much!!
    The best tutorial on Iphone development on the web…
    When the next lesson is coming?

    ~Vish

  31. Oliver on August 24, 2009 2:59 pm

    Brilliant, it’s solves my problem mostly, but when I added extra tabs I can’t access any of them from the “more” tab. Still when I move them onto the tab bar they load fine.
    Any ideas on how I could get them to load from “more”?
    Thanks
    Oliver

  32. James on August 26, 2009 5:39 pm

    Great tutorial
    Any idea when the next part will be ready and how many parts overall?

    Thanks

    James

  33. John on August 31, 2009 12:44 pm

    Great tutorial, compliments.
    Can you helpme to create sqllite and confogure it in application?

    Thanks :-)

  34. John on August 31, 2009 1:13 pm

    “In the next installment we will be creating our SQLite database and adding some feeds and groups, we will also be setting up the BrowseViewController to handle multiple levels of groups with only one view (Drilldown) and we will also be looking at storing images in a SQLite database with a BLOB field.”

    Please, how to make it?

    Thanks.

  35. Flo on September 4, 2009 1:24 pm

    Oliver, same problem for me. I also figured out that when you move an item from the “more” table to the tab bar, open it and then move it back to the “more” table, you can also open it from there. Weird… Any solution?

Trackbacks

  1. IPhone tab bar problems
  • There are no trackbacks for this post

Write a Comment