If this helped you, please consider downloading my FREE iPhone app "Hydrate Yourself"and leaving a review.


There are a whole bunch of tutorials to create a Navigation Controller inside a TabBarController with XCode3.  But there are a number of small changes in XCode4 which can trip up someone not familiar with them.  So this guide is essentially an updated version of those other guides, designed to create the same simple demo using the new tool.

1. Create a new Project
We are going to start with a Tab Bar Application, so go ahead and create a new project as usual. I'm calling mine Navigation Tab Bar.

XCode4: UITabBarController with UINavigationController using Interface Builder_please


2. Select the Tab Bar
Click MainWindow.xib and you should see the integrated Interface Builder. On the left is a section called Objects, select the Tab Bar Controller.

XCode4: UITabBarController with UINavigationController using Interface Builder_please_02


3. Add the Navigation Controller
On the right side of your screen click the Utilities box. This is new in XCode4 and should look familiar if you've been using XCode3. At the bottom find the Navigation Controller and drag it into the Tab Bar in the middle of your screen.

XCode4: UITabBarController with UINavigationController using Interface Builder_please_03

 
XCode4: UITabBarController with UINavigationController using Interface Builder_职场_04
Here you can see I added it as the middle Tab
4. Configure new tab
Back in the Objects section you can now expand the Navigation Controller and select the View Controller object. Now on the right side you can select the Identity Inspector at the top and put in your UIViewController's class name. For the purpose of this demo I am calling it "YourNewViewController" which we will make at the end.


XCode4: UITabBarController with UINavigationController using Interface Builder_职场_05
 
Once that is done switch to the Attribute Inspector and put in the name of your XIB file in the NIB Name section. Note: do not add the .xib extension, this will cause a runtime crash.
 
XCode4: UITabBarController with UINavigationController using Interface Builder_职场_06
 
5. Create the new UI View Controller
This is probably familiar to you already, create file from the File menu and select a UIViewController subclass.
 
XCode4: UITabBarController with UINavigationController using Interface Builder_please_07



XCode4: UITabBarController with UINavigationController using Interface Builder_please_08
Be sure to select With XIB for user interface



XCode4: UITabBarController with UINavigationController using Interface Builder_please_09
Use the same file name as you used before.


6. Add an IBAction
We will be adding a button to push a view onto the navigation controller.  Open up the header file and add an action.


XCode4: UITabBarController with UINavigationController using Interface Builder_职场_10
I just added the buttonPushView line
 
Switch to the implementation file and add another few lines for a simple call to pushViewController.


XCode4: UITabBarController with UINavigationController using Interface Builder_职场_11
This is the simplest pushViewController line I could come up with


7. Add a button
Select YourNewViewController.xib in the file menu. Adding a button is a simple matter of dragging/dropping from the bottom right Objects section.

Next you link the button to the new IBAction by right clicking the button and dragging the arrow to File's Owner, in the Placeholders section. Select buttonPushView.
 
XCode4: UITabBarController with UINavigationController using Interface Builder_iPhone_12
 
8. You're done!
All thats left now is the build and run (Command-R).  Select the second tab to see your new Navigation Controller and click the button to see it in action.
 
XCode4: UITabBarController with UINavigationController using Interface Builder_休闲_13      XCode4: UITabBarController with UINavigationController using Interface Builder_职场_14
 
转自:http://blog.willwinder.com/2011/05/xcode4-uitabbarcontroller-with.html