Implement tag navigation using nib
You can use the project template tabbed application to create tag navigation applications in xcode 4.5. We can choose storyboards or nib technology. The creation of the story board is simple, but this implementation method shields many technical details of tag navigation. To make it more beneficial for us to learn, we adopt nib technology and storyboard technology respectively in the implementation process. First, let's take a look at the implementation process using nib technology.
Use the "tabbed application" template to create a project named "tabnavigationnib. Do not check "use storyborads ".
The job has two screens, firstviewcontroller. h and firstviewcontroller. m are the view control items of the first screen, and icons are required for the retina display screen. The image of the second view is similar to that of the firstview.
In order to correspond to our application, we need to make some modifications to the View Controller generated by the template. First, modify the View Controller name. It is best to use the tool provided by xcode to modify all code with dependency (except for the hard-coded string part in the process ). Open firstviewcontroller. h file. In the code, double-click the class name firstviewcontroller to be renamed, right-click refactor → rename, and a dialog box with the new name will appear, enter the class name "heiviewcontroller" to be modified, and a preview dialog box is displayed. If there is no problem, click "save" to save the modification. Change secondviewcontroller to jiviewcontroller in the same way.
Because two sets of view controllers are generated in advance using the project template, city information requires three view controllers, we can add another view controller "liaoviewcontroller" based on the two view controllers generated by the template, and select File> New> file… from the menu... In the file template, select IOS> Objective-C. The new file dialog box is displayed. In the class project, enter "liaoviewcontroller", select uiviewcontroller in subclass of, and check with XIB for user interface.
After the View Controller is created successfully, you need to add the label bar control at the bottom of the view to make the view usable in the label bar. This control occupies 49 points, select the view to open the property checker and select "buttom bar" as the tab bar. A black box will appear below the view. The purpose of this operation is to remind us not to place other controls in the label bar when designing the screen. Otherwise, this control will be blocked by the label bar during running.
Design the three views as shown in the following figure, drag some label controls, place the positions, change them to the name of the city shown in the figure, and then modify the color of the view background.
You also need to change the icon, delete the four original icon files, and add the icons folder of the source code project provided in this book to this project.
Next let's take a look at the code section. The code about the application delegate object appdelegate. m is as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];UIViewController *viewController1 = [[HeiViewController alloc] initWithNibName:@”HeiViewController” bundle:nil];UIViewController *viewController2 = [[JiViewController alloc] initWithNibName:@”JiViewController” bundle:nil];UIViewController *viewController3 = [[LiaoViewController alloc] initWithNibName:@”LiaoViewController” bundle:nil];self.tabBarController = [[UITabBarController alloc] init];self.tabBarController.viewControllers = @[viewController1, viewController2,viewController3];self.window.rootViewController = self.tabBarController;[self.window makeKeyAndVisible];return YES;}
This part of code is called when the application is started. If it is built using a storyboard, this part of code is not found in this method. The delegate object has a property tabbarcontroller, which is of the uitabbarcontroller type. Uitabbarcontroller is a label bar View Controller. Its important attribute is viewcontrollers, which is an nsarray array type, used to store all view controllers of various modules controlled by the label bar View Controller. Then, use self. Window. rootviewcontroller = self. tabbarcontroller to add the label bar View Controller to the Root View of the window. Relationship between window, label bar controller, and module View Controller.
The window in the application delegate object is an instance of uiwindow, and each application has only one uiwindow object. As the "window" of the application, the Root View Controller in the "window" is the tab bar controller.
Let's look at the code of the View Controller heiviewcontroller. m of a module:
-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) attributes {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {self. title = @ "Heilongjiang"; // nslocalizedstring (@ "First", @ "First"); self. tabbaritem. image = [uiimage imagenamed: @ "Hei"];} return self ;}
This is the construction method of the View Controller. In this method, self. Title sets the label text in the label bar, And self. tabbaritem. Image sets the icon in the label bar. The other two view controllers are similar.
The result after the code is compiled.