iOS Development: Switch views with tab bar
The previous article referred to the switch between views in a multi-view program, using the tool Bar, or changing the content view in the root view controller with code based on the triggering event. This time, let's talk about switching views, but this time we're using tab Bar.
This time to write the program to run the effect is this: there are several icons at the bottom, each icon corresponding to a view. Each click of an icon will open the corresponding view. For example, we do a good job of the program effect:
Each tab bar has a color-corresponding view.
To figure out how to switch views using the tab bar, we'll start by creating our program from empty application.
1, run Xcode 4.2, create a new empty application, the name is tab Bar application, other settings such as:
2. Add an icon file for the project:
The icon file to be added here is used to customize the tab bar. First create a new group, select File-new-new Group, and then rename the new group to icons. Then, drag the prepared four icon files into group and select copy items in the popup window ... (if needed), such as:
4. Create four View controllers:
Select tab BAR Application This group, then select File-new-new File, in the pop-up window, choose Cocoa Touch on the left, select Uiviewcontroller subclass on the right, then choose Next, In the pop-up window, enter the name Blueviewcontroller and select with Xib, such as:
Then select Next, choose a location, click Create, and create a viewcontroller. Create three more in the same way, with the name Greenviewcontroller,redviewcontroller,yellowviewcontroller.
5. Create Tabbarcontroller.xib:
Select tab BAR Application This group, then select File-new-new File, in the popup window, select User Interface on the left, and the right to empty:
Then click Next, enter the name Tabbarcontroller in the pop-up window, select the location and tap create.
Then, click on Tabbarcontroller.xib in group, you will find that unlike blueviewcontroller.xib, there is not a window like view, do not worry, we drag a tab Bar Controller into the inside:
6. Select File ' Owner in, open identity Inspector, and select Appdelegate in the class column:
In this way, we can create outlet mappings from Tabbarcontroller.xib to Appdelegate.
7, open Assistant Editor, ensure that assistant editor is open AppDelegate.h, on the left of the tab Bar Controller, control, to create a map in AppDelegate.h:
Then enter Rootcontroller in the pop-up window and click Connect:
Open APPDELEGATE.M and add code in the Didfinishlaunchingwithoptions method:
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{ Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]]; Override point for customization after application launch. [[NSBundle Mainbundle] loadnibnamed:@ "Tabbarcontroller" owner:self Options:nil]; [Self.window AddSubview:self.rootController.view]; Self.window.backgroundColor = [Uicolor whitecolor]; [Self.window makekeyandvisible]; return YES;}
8. Click Tabbarcontroller.xib, drag two tab bar item to tab bar:
9. Select the first view Controller, open the identity Inspector on the right, and select Blueviewcontroller in class:
Then, open attribute, select Blueviewcontroller in nib name:
Do the same for other view controllers, which are set to Greenviewcontroller, Redviewcontroller, Yellowviewcontroller.
10. Set tab bar icon and text:
Expand Blue View Controller, select the tab Bar Item, and open the attribute, such as:
Badge properties: The text you set will appear as a red icon, for example, this tab displays the Mail view, and you can use badge to show how many unread messages you have.
Identifier property: This property corresponds to the drop-down menu, if you choose is not custom, such as favorite, then the tab bar name and icon are set. We choose custom here.
In title, enter blue and select Blue.png in image.
Perform a similar operation on other tab bar item so that the entire tab bar looks like this:
11. Now click. xib, select View, open attribute Inspector, and change its background color to blue. Then, set the bottom bar to tab bar in simulated metrics:
The same settings are set for Greenviewcontroller, Redviewcontroller, and Yellowviewcontroller, but the background color should be set to match its name.
12, finished, run a bit, see the effect bar:
Transferred from: http://www.howzhi.com/group/iosDevelop/discuss/1936