IOS - Programming : uses UItabbarcontroller and UInavigationcontroller together
UITABBARCONTROLLER WITH UINAVIGATIONCONTROLLER USING INTERFACE BUILDER
I’ve seen a good bit of sample code that shows how to implement using a UINavigationController in a view controller that is managed by a UITabBarController, but I haven’t seen much on how to do it with Interface Builder. Turns out that it’s pretty simple and I’m going to show you how.
To get started, create a new project in Xcode. Select the Tab Bar Application template and click Choose….
Name the project, TabBarNavigator and click Save
Change The Controller Type
- In Xcode, expand the Resources group in the Groups and Files view and double-click MainWindow.xib to open the xib in Interface Builder
Pushing a New Controller
Before we create a new view controller, let’s set up our FirstViewController to create an event that will trigger pushing the new controller. Since our FirstView was not created with a xib, let’s create one and then add actions and outlets to it.
- In Xcode, right-click or ctrl-click the Resources group and select Add | New File…. In the ensuing dialog, selectView XIB in the User Interface section of iPhone OS templates and click Next. Name the XIB FirstView and clickFinish.
- Double-click the new XIB to open it in Interface Builder and select the Identity tab after you have made sure thatFile’s Owner is selected. Change the Class field to FirstViewController.
- Ctrl-click and drag a connection from the File’s Owner object to the View object in the main window for our XIB and select view in the ensuing pop up menu.
- Open the file FirstViewController.h and add an action so that the header looks like this:
- Open the file FirstViewController.m and implement the action so that the code looks like this:
Note: You can safely delete all of the view controller template code which I have done in this example. Don’t worry. You won’t hurt anything.
We will come back to the push implementation after we have created our new view controller that we are going to push.
Create the New View Controller
We are now going to create the new view controller that will be displayed when we tap our “Push” button in theFirstViewController.
- In Xcode, right-click or ctrl-click the Classes group and select Add | New File…. In the ensuing dialog, selectUIViewController subclass in the Cocoa Touch Class category of the iPhone OS templates. Make sure With XIB for user interface is checked and click Next. Name the file NewViewController.m and click Finish.
- For organization purposes, move the newly created NewViewController.xib file into the Resources group. Then double-click it to open it in Interface Builder.
- In Interface Builder, drag a UILabel onto the view and give it the title “New View Controller”. Save the file and switch back to Xcode.
- Open the file FirstViewController.m again and implement controller push code so that it looks like this:
Don’t forget to #import “NewViewController.h”
- To connect our button to the push action, double-click the FirstView.xib file in the Resources group to open it in Interface Builder.
- Ctrl-click and drag a connection from the “Push” button in our view to the File’s Owner object and select push: in the ensuing pop up menu. Save your changes in Interface Builder. Switch back to Xcode and Build & Go.
- Additionally, you can give your New View Controller a title by implementing -viewDidLoad. Just open yourNewViewController.m file and uncomment the -viewDidLoad code making it look like the following:
And that’s pretty much all there is to it. There are a lot of steps here, but I think you’ll see that setting up your application to implement both a UITabBarController and a UINavigationController is pretty straight forward.
Conclusion
How best to organize your views in an iPhone application is a design process. Often applications will have a lot of data for your users to access and it will be up to you to find the best way to organize it. Being able to utilize both a tab bar and a navigation controller can really help in this process. Until next time.
Nhận xét
Đăng nhận xét