UITabBar iPhone Tutorial

September 28th, 2008 Posted by: (ELC) - posted under:Articles

This tutorial will show you the basics of using the UITabBar and UITabBarItem controls when programming for the iPhone.

I will be showing you step by step in the video how to create a project from a UITabBar template and add additional UITabBarItems as well as additonal views to be displayed with these items.  Here is a brief explanation of each step:

1. Create a new project from a UITabBar template

This is pretty straight forward.  We will be using Apple’s default UITabBar template to start our project.  This is a fully functioning app.  You can click Build and Go to see it run.

2. Open Interface Builder (click on mainWindow.xib) and Add addtional UITabBar items

To add additonal UITabBarItems, you simply drag them from the library on to the UITabBar.  For each new item, you must specify the name of the view that will be shown when the item is clicked.  This is done by clicking on the tab bar item and clicking Tools -> attributes inspector.  There should be a box titled “Nib Name”.  Whatever you enter here will be the name of the view that will be loaded when this tab is clicked.

So for this tutorial, we entered ThirdView.  So, naturally when we created our view to be loaded here, we saved it as “ThirdView.xib”.  This is one Gotcha when working with these components.

3. Changing the text of the UITabBarItems

This is took me a while to figure out.  To change the text, image, or style of the UITabBarItem, double click on it.  You should now be able to access these settings in the Attributes Inspector.

4. Set the Background Color For Views 1 and 2

This was pretty easy.  For the first view, we simply clicked on it and clicked the box next to “Background” in the Attributes Inspector.  Next, we did the same to the secondView by opening up SecondView.xib in Interface Builder.

5. Create ThirdView

We need to create the third view to be loaded when the user clicks on the UITabBarItem that we inserted.  This was done inside of Interface Builder by clicking File -> new and selecting View. The next thing we did was drag a label from the library and add it to the view.  Finally, we set the background color like we did in the other views.

Be sure when you save this view, you name it ThirdView.xib (this corrisponds the name that we put in the “Nib Name” box in step 2).  Also, be sure to save this view in your projects folder (sometimes Interface Builder does not navigate here by default).

6. Connecting ThirdView to the ViewController

In this step we connect the ThirdView to our FirstViewController.  Notice that all of the views get connected to this view controller.  This will handle all of the UI components put on to each view. *Note: this step can’t be done until ThirdView is saved and added to the project.

So to connect this view to FirstViewController, simply click on the File’s Owner object and select FirstViewController from the drop-down in the Identity Inspector.

The last thing we need to do is connect the view.  So control-click from File’s Owner and drag the line to the View object. The word “View” should pop up.  Click on it and the views are now connected.

That should be it! Click Build and Go and you are done.

Well, I hope you guys have enjoyed the video tutorial.  I know that the audio is kind of low.  This is because I used my computer’s built in microphone and it’s not the greatest.  I have since purchased a new microphone (thanks again to all who have donated thus far) and future tutorials will have much better audio quality.  I would LOVE to hear your feedback on the videos.  I’m a total noob when it comes to screencasts and would love your constructive criticism.  Thank you all and Happy iCoding!

Download  the source code for this tutorial here

  • Screem

    Dude, that was an excellent tutorial and an awesome job on the video. I am glad that someone is doing this. I am having a hard time finding good tutorials on iphone programming. I cant wait to see your future tutorials.

  • George

    Very nice tutorial for XCode starters. It helped me a lot to learn the GUI. Can’t wait to see more.

  • Jagadeesh

    Amazing dude….awsome…tutorial…. Thanks a lot :)

  • Scott

    Thanks Brandon. Now I would like you to go ahead and make a tutorial for a nested table view. This will allow me to go ahead and write one too. Your readers will also be able to go ahead and learn. Then we will all go ahead and use our iphones to go ahead and have fun.

  • http://www.icodeblog.com Brandon

    @Scott

    LOL, did I say “go ahead” too much? Haha I’ll watch that in future videos. Thanks for pointing that out.

  • http://fernyb.net fernyb

    Good job! Keep up the great work… Thanks.

  • pfargo

    Impressive first screencast.

  • http://www.space-equilibrium.net Urza5886

    good tutorial…any chance these could be uploaded to itunes so i could watch them on the go, if not could you make them downloadable so they could be used as reference material. its easy for me to learn if i keep repeating these over and over again.

    Keep up the good work :)

  • derda

    very good job. thank you!

    @Urza5886 just open up the rss-feed as xml (view feed xml) and click on itunes (right side, actions)

  • face

    GREAT, really STUNNING.

    May you please add a tutorial on how to simply read a sqlite database
    (with for example this schema)
    A1 A2 A3
    B1 B2 B3
    and populate some multiline field text boxes (hope the term is correct)
    field1 > A1
    field2 > A2
    field3 > A3
    and move forward
    field1 > B1
    field2 > B2
    field3 > B3
    and backword

    and learning us also how to create a preferences database where to save program’s preferences (for example at startup load data from last record seen)?
    hoping not ask too much :)

    anyway, again, great tutorials,
    thank you very much for sharing …

  • http://www.jordibares.co.uk Jordi Bares

    I would like to hear a bit more about multiple XIB files versus one file containing multiple views. Benefits to follow one of the other strategy and of course the implications on the coding side.
    I understand a more modular approach would be desirable but does it bring far more work on the coding side|?
    Thanks for your tutorials.

  • Filatier

    Just to say you’re doing a really great job here !

    Please keep going on, it’s so interesting and easy to understand.

    Thx.

    MF

  • http://www.icodeblog.com Brandon

    @face,

    Have you checked out my sqlite tutorials. Also, an application preferences tutorial is on the way…

    @Jordi

    I’m not sure I really understand your question. Could you please rephrase it?

    @Filatier

    Thanks, I’m happy it helped.

  • Chet

    Your tutorials have really been great Brandon and are very much appreciated.

    One thing I have been trying (and failing) to do so create an app that uses a single view list, based on your sqlite3 tutorial, that when an item is chosen, will transition to a tabbed view with different fields presented across each tabs view, all related to the primarykey chosen on the first view.

    Any ideas how to make this happen or can it even happen?

    Thanks.

  • http://www.kassenaar.com/blog Peter Kassenaar

    Hi Brandon,

    Great first vid. Keep it up!

    A few suggestions to further improve the quality of future tutorials:

    *) At the start of the video, show the viewers what the final application will look like (i.e., the end product). Doing so will enable the viewer to place your actions more in perspective with the ultimate goal. Also they can at any point in the video build a ‘mental map’ of what is needed to create the application *they have already seen* (and compare this to what is done so far).

    *) Do a little less zooming and panning. It is very good to zoom in on code and UI-elements that have focus, but is gives the viewer a restless feeling if the ‘camera’ seems to swerve all over the screen. Try to bring some more rest in the video.

    Otherwise – all compliments.
    Looking forward.

    Peter.

  • Adam

    Great Stuff. Thanks for the hard work.

  • Lefty

    Amazing first video, thanks for such a site and resource. Happy iCoding!

  • Alex

    Really nice video tutorial. The video quality was good and you spoke clearly and went through the steps at a good pace.

    One thing I’d like to be able to do is download the .mov file so I can refer back to it without having to reload the movie in my browser if possible

    I’m looking forward to seeing some more tutorials

  • Tom

    Fabulous work! I’ve been trying to figure this out for a while, and it’s so hard to light up it’s simple logic behind the rather complicated Interface Builder. どうもありがとうございます。

  • http://www.nagel-photo.com Dan

    Great Job,

    I really appreciate you taking the time, This is so helpful for me as a beginner. I did have a problem though, When I do the final build and run the app I get an eror on the third tab: __Terninating_Due_To UnCaught_Exception__

    Any Idea where I went wrong?

  • http://www.nagel-photo.com Dan

    Hi Brandon,

    My Bad, I guess I didn’t actually click on that “View” in that last pop up window.

    Keep up the good work, I can’t wait to try your next one.

  • Forge

    Thanks for the tutorial. I’ve followed a few tutorials and they all work fine, but I’m making my first tab-bar app, and got a bit stuck. Following your tutorial, that all works fine, but adding an outlet and connecting to the label (with plans to write code to alter the text later), its crashing.

    Heres a video of me demonstrating:
    http://www.vimeo.com/1889825
    Clearly I’m doing something stupid, or not doing something, if anyone knows, let me know.
    Cheers!

  • bearc0025

    Awesome! Thanks! I think the video format is more effective than the text.

    BTW, if you make more video tutorials, can you create a category for them?

    Again, great job!

  • Pad

    Excellent work!

    I didn’t understand where you told X-Code to show the third view when the user clicks on the third button in the UITabBar…

  • http://www.icodeblog.com Brandon

    @Pad,

    This part was a little tricky. First I clicked on the “3rd View” tab. In the Attributes inspector, I typed in ThirdView in the nib name box. This means that it needs to load a view named “ThirdView”. All I did was create another view and named it “ThirdView” and it was automatically loaded by Xcode.

    Does this help?

  • Marco from Ecuado

    great job here !

    thenx for the tip..

    i like to create a ibook program.. any other tip.

    thenx agen

  • Dave

    Great tutorial! Quick question – does MainWindow.xib’s File Owner become the File Owner of SecondView.xib? I tried taking SecondView to the next level by setting it’s File Owner to a seperate .m ViewController file but it doesn’t work…is there a difference between a XIB that’s launched standalone versus launched from the Tab Bar?

  • Eric

    Excellent Tutorial! Now if only I could figure out how to make a table with navigation capabilities inside of each of the tabbed views (exactly how it is in the iPod.app application). I’ve been trying to do this for a few days now and I’m going crazy. Any thoughts?

  • Eric

    Nevermind, I figured it out.

  • Dave (a different Dave)

    Thanks for the great tutorial!

    I am having a problem similar to the other Dave.

    I think I am missing something fundamental about how IB ties different views and controllers together.

    Can all three views share the same controller? Or do they each need their own? Could it work either way? I set mine up with all three views using the same controller.

    I’ve set mine up with three different views each in their own xib. (it seems like the apple template doesn’t actually create a first view, just a label… that confuses me a bit… so I created a FirstView.xib and load it in the same way that the template loads SecondView.xib and your tutorial loads ThirdView.xib)
    My first view loads some data from NSUserDefaults and puts it in some labels on the first view. That all works fine.

    BUT… on my second tab, I have a button that is intended to do some work. When I try to wire it up to call the buttonPressed method in the FirstViewController, it doesn’t work. FirstViewController is the only view controller… all three views use it.

    When I test things and press the button on tab two, the error I get is:
    2008-10-12 20:40:07.195 ContactDeduper[1064:20b] *** -[UIViewController buttonPressed:]: unrecognized selector sent to instance 0x450b60
    2008-10-12 20:40:07.196 ContactDeduper[1064:20b] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** -[UIViewController buttonPressed:]: unrecognized selector sent to instance 0x450b60′

    …and I can’t figure out what’s wrong.

    I’d LOVE to see a “part two” of this tab tutorial that adds a control to each tab and shows how they get wired up!

  • Dave (a different Dave)

    Progress!

    Buttons were working on the first view, but not the second or third, but I couldn’t find anything different between them.

    I finally did…

    When I opened MainWindow.xib, and selected Tab Bar Controller, I could see the 3 controllers for the 3 views. The first one had a type of FirstViewController, but the other two had a type of UIViewController.

    This makes sense… the controllers for the 2nd and 3rd tabs were generic tab controllers… so couldn’t response to buttonPressed messages because that method is defined in FirstViewController.

    And, I think it answers a questions that has been nagging at me… although I have created a single class called FirstViewController (which I should rename MultiViewController or something like that)… each view gets a different instance of that class… and that’s what is shown under the TabBarContoller. Right?

  • http://www.icodeblog.com Brandon

    @Dave (a different Dave)

    That’s correct. The Views for each tab only need one view controller. You will connect all of the IBActions and IBoutlets to this controller.

    You are right, I should have named it something like MulitviewController but I just kept it as FirstViewController as that was the default.

    Thanks for watching…

  • Sean

    I’m trying to use this as a starting point for building a 4 tabbed application that opens a different webpage in each view.

    I can get it to display the webpage on the MainWindow and the tabs navigate to the other views but the other views contain only simple text labels from IB. How/where do i create the other 3 webviews and how/where would they be called??

    BTW – like everyone else here… WOW.. these tutorials have helped me with learning to code for the iPhone even more than Mountain Dew+coffee…. thanks!!

  • http://none frank

    how do you change the color of the UITabBar ?
    it appears like you can do it from interface builder.. but you cant, on launch the background is always black.

  • leo

    Hey..great tutorials…
    Is there any way to switch betweeen a NavigationController View to a TabBarController View?

    I’m writing an app that has an initial login screen, then should switch to a screen with a tab bar, but am unsure about the right way going about doing that.

  • JeremySpouken

    Awesome tutorial! thank you so much.

  • Gie

    Hi! Nice tutorial. I have a question similar to Leo.

    How can I switch to different tab views without clicking on the tab items below? Example I am in the first view (first tab), then when I click a button, I must change the next view (second tab). How?

    Thanks in advance. :)

    -angie

  • Gie

    Have another question:

    Is there any event handler to know if a tab is selected (clicked)?

    Thanks! :D

  • Vitalyp

    Sean – any luck making that 4 tab website browsing app?

    I’m working on same thing, but not having any luck getting the website to show up.

  • http://versatilemind.com André

    Hey there!

    Just a quick question … should I have multiple ViewControllers (one for each tab, each view with his own view controller) or is it recommended to have just one view controller (like your firstviewcontroller in the sample app) for all the views in all the tabs

  • sandeep asrani

    Hi i am new to apple iPhone development but is it possible to create a tab bar with an identifier set to favorite but change the text under it…. For example i want to use the Favorite identifier so the image is set to ‘star’ and text to favorite can i keep the image as it is and only change the text… or do i need to then create another star to use it…..

  • Tylor

    Awesome! Thanks so much! You are amazing. I wish you the best success with this blog and hope that it really takes off!

  • Tarun sharma

    hello

    I am working on an iphone application in which i want to load diffrent views on tap of diffrent tab.On my first view i have a table loaded through databas.I want when user tap on another tab table present in second view got loaded .But when i set datasource and delegate of second view table to its file owner and run the application.when user tap the second tab application gives error “Error due to uncaugh exception”.Can someone help me how to use table in tabbar application in diffrent view.

    Thanks
    Tarun sharma

  • http://www.onemoretap.com XCool

    Thanks for the stunning video tutorial! Love to see more from helpful developers like you getting us newbies started. ;)

  • Ole Hansen

    Hi

    These tutorials put me on the fast learner track. Great stuff!!

    My question: When loading the second view created by apple, there is a place holder for the tab bar. This place holder does not show up in the ThirdView that is created during the tutorial – how come? Can I make it appear in ThirdView as well?

    Thanks,

    Ole

  • Ole Hansen

    Figured it out:

    Select the view in ThirdView
    in the attributes inspector in simulated metrics enter:
    status bar: gray
    Top bar: none
    Bottom bar: Tab bar

  • http://www.ramsesmoya.com Ramses

    Great tutorials – wanted to let you know that I have learned a world from them… thanks so much for sharing.

    RM

  • owen

    Hallelujah!

    Thanks for the tips regarding the tab bar exceptions.

    I had been working on a project (from a different tutorial) and after renaming some items the app was crashing on the third tab. I thought I updated all the places – but you helped me find the one I missed. Thanks!

  • Hitesh

    Is there any way we can get which tabbar item is tapped? And we can perfom any activity on tapping of first and second tab but not for third.

  • scrudrv

    Hi Brandon

    Thank you very much for all your Tutorials. This one was awesome. Screencasts really speed up the process of getting new information.

    Thanks again and keep up the good work!

  • http://www.twitter.com/GuitarKat Kathleen

    Thank you for posting this excellent tutorial. I was always confused regarding the tab bar but this made it much more clearer.

    Thanks again! :)

  • mamatha

    its a great tutorial
    thanks alot..

  • http://www.unisynergygroup.com Eric

    Great screencast Brandon. Thanks!

    BTW, what’s the recorder you used? Thanks again :-)

  • http://www.digitalvanilla.com David

    good tutorial.
    I wish to recreate the top tabs that Facebook has it.
    I saw inside the .ipa and they are buttons.
    Where do I find a tutorial how to reproduce this effect?

  • James

    Hey Could you please show us how you use the controller with these views? I’ve tried connecting the labels to my firstviewcontroller and it just doesnt run :(

  • http://www.google.com Mohit

    Hi

    This is nice tute but i was wondering if u could have provided some more function of tab bar like displayinh images on tab bar items…

  • http://www.kridan-consulting.com Dwayne King

    I have a simple question (newbie obviously)

    I decided to play with this app a little and add a fourth item (a search page). The UI loads properly, and selecting the tab bar icon shows the page, but if I type something in the search field, I can never get the keyboard to go away (and it covers the tabview so I’m stuck there)

    What have I missed?

    Thanks in advance.

  • http://www.alhnuf.com/ ameer
    • me mine

      You Qoran fricks you hate what is human and beautiful. You are out there to destroy the world because you have never produced anything good to serve humanity.

  • mora
  • http://www.robinkanters.nl Robin Kanters

    Hey man!

    Great tutorial, I totally understand it now. However, you went a little quick through it, so I had to rewind a couple of times…

    Robin Kanters
    the Netherlands

  • http://- Harris Zacharis

    I am working on a recipe app at this time…
    My question is:
    The app is navigational. After navigating to the right recipe, the recipe appears on a uitext view. What iI want to do is to implement a button which will add the recipe to a “favorites” tab.
    Is there a specific function to help me accomplish that?
    Any help would be appreciated.

  • http://www.noisediary.com Robert

    Thank you so much – I am looking for easy-2-use tutorials and you hit the nail right on the head.

    Thank you again!

  • Salman

    How to Add TabController in an exitsing UIViewContorller

  • peetonn

    hello!
    i’m curious about method you added new views to the tabbar. why did you create new file and choose “View”, instead of simply dragging new view from the library directly to the tabbar control? are these ways equivalent or not?

  • http://www.tenjordan.com/airjordanolschool Jordan OL’ School

    Thanks for your great website, i love it very much. Welcome to visit our website too.

  • Pingback: พื้นฐานการสร้าง App โดยใช้ UITabBarContrller | iPhone developer in Thailand

  • Wshelor

    This is fantastic!  I’ve been so frustrated trying to get this to work- thanks for the guide! 

  • Wshelor

    This is fantastic!  I’ve been so frustrated trying to get this to work- thanks for the guide! 

  • Sergio E Abraham

    Hi!
    I’m having issues when I try to use my own .xib files on each tab. I’m frustrated at the moment. It’s been 24hs that I’m having the same error (In fact nothing is shown in debugger but a ‘Received SIGKILL’ )

    How can I make an nib file to be shown by a tab? I already followed these instructions several times, and I’ve checked the connections between the nibs (I’ve compared mine with those example views you get when you start a tab based project.. 
    And still nothing :(
    Sometimes I get only a black screen with no controls at all

    Could you tell me what I should prepare my nib files in order to be used by the mainwindow tab controller?

    Thank you very much
    Sergio 

  • Sergio E Abraham

    Hi!
    I’m having issues when I try to use my own .xib files on each tab. I’m frustrated at the moment. It’s been 24hs that I’m having the same error (In fact nothing is shown in debugger but a ‘Received SIGKILL’ )

    How can I make an nib file to be shown by a tab? I already followed these instructions several times, and I’ve checked the connections between the nibs (I’ve compared mine with those example views you get when you start a tab based project.. 
    And still nothing :(
    Sometimes I get only a black screen with no controls at all

    Could you tell me what I should prepare my nib files in order to be used by the mainwindow tab controller?

    Thank you very much
    Sergio 

  • wiegeabo kilijabob

    So where’s the video?  Because I don’t see a link anywhere.

canakkale canakkale canakkale balik tutma search canakkale vergi mevzuati bagimsiz denetim vergi mevzuati ozurlu engelliler