The iphone uses UINavigationController to navigate between two pages (bottom)

Source: Internet
Author: User

Before renewal)
 
Step 7: UINavigationItem and UIBarButtonItem
Generally, each (or any) View Controller can define a UINavigationItem. The UINavigationItem class implements these functions. This class contains the following table:
Button on the left
Title View
Button on the right
LeftBarButtonItem
TitleView
RightBarButtonItem
When the View Controller to which it belongs is called at the top of the stack controlled by the navigation controller (that is, when the View Controller is about to be displayed), the system automatically displays the UINavigationItem of the View Controller, developers do not need to write any code to call UINavigationItem, but they must be set before. The custom left and right buttons in the code above are described as follows:
Custom return button: by default, the text on the return button on the next page is the title of the previous page. You can modify it to another text in the program. Remember it! The return button is placed on the View Controller of the previous page. Therefore, you must modify it in the implementation file (*. m) of the View Controller corresponding to the previous page. For example, the above lvyouAppDelegate. m contains the following code: (green code in the figure)
[Plain]
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {
......
NavController = [[UINavigationController alloc] init]; // initialize the navigation Controller
// Define the return button named "all cities. No target or action is required for this button.
// Because the system has implemented the return function. Style is the display Style
UIBarButtonItem * backButton =
[[UIBarButtonItem alloc] initWithTitle: @ "all cities"
Style: UIBarButtonItemStyleBordered
Target: nil action: nil];
// Set the first view controller to be displayed when the application is started. Here, cityViewContrl
CityViewController * cityViewContrl = [[cityViewController alloc] init];
CityViewContrl. title = @ "Travel Guide"; // set the title of the first View Controller
// Set the return button
CityViewContrl. navigationItem. backBarButtonItem = backButton;
[BackButton release];
// Push the first view controller to the stack.
[NavController pushViewController: cityViewContrl animated: NO];
[CityViewContrl release];
// Place the navigation (Label bar) Controller under the Window
// [Window addSubview: navController. view];
[Window addSubview: tabBarController. view];

// Override point for customization after application launch.
[Window makeKeyAndVisible];
Return YES;
}

Add system buttons and left and right buttons in the navigation control bar.
First, add a "discount Info" button in the navigation bar on the first page. Remember as described above! The return button is placed on the View Controller of the previous page. Therefore, you must modify it in the implementation file (*. m) of the View Controller corresponding to the previous page. You must add a "discount Info" button in the navigation bar on the first page (obviously, this is not the previous page button, but the left button in the navigation control bar "), therefore, the implementation file of the current view controller (*. m), that is, cityViewController. m. The original Code contains the following code: (green code in the figure)

[Plain]
-(Void) viewDidLoad {
// Create a style button with a border. After pressing it, call the discount method on the View Controller.
UIBarButtonItem * discountButton = [UIBarButtonItem alloc]
InitWithTitle: @ "discount Info" style: UIBarButtonItemStyleBordered
Target: self action: @ selector (discount :)];
Self. navigationItem. leftBarButtonItem = discountButton; // set the button to the left
[DiscountButton release]; // releases the memory.

UITabBarItem * item = [UITabBarItem alloc]
InitWithTitle: @ "Travel Guide"
// InitWithTabBarSystemItem: UITabBarSystemItemBookmarks
Image: [UIImage imageNamed: @ "GoldenGateBridge.png"]
Tag: 0];
Self. tabBarItem = item;
[Item release];

[Super viewDidLoad];
}

Next, add a "system button" (right button) in the navigation bar on the second page. According to the analysis above, this is also not as good as the return button, therefore, the implementation file of the current view controller (*. m), that is, CityDetailViewController. m. The original Code contains the following code: (green code in the figure)
[Plain]
-(Void) viewDidLoad {
CityName. text = city; // obtain data from the previous page
// Create a system add button. After pressing this button, call the add method on the View Controller.
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]
InitWithBarButtonSystemItem: UIBarButtonSystemItemAdd
Target: self action: @ selector (add :)]; // call the add method on the View Controller.
// Set as the right button on the control bar of the navigation Controller
Self. navigationItem. rightBarButtonItem = rightButton;
[RightButton release];

[Super viewDidLoad];
}

Step 8: UITabBarController)
The label bar controller is a view controller that uses arrays to manage controls. These managed view controllers can be navigation controllers or general view controllers. In addition, these view controllers are equal, unlike the view controllers managed by the navigation controller ". By default, the title of each view is displayed on the tab bar, for example, five labels are displayed.
Up to five tags can be displayed in the tag bar of the iPhone OS.

When you select a View Controller, this view controller is executed. When you select another view controller, the status of the current view controller is retained. It is very similar to Microsoft's multi-window concept.
Relationship: equal relationship between upper and lower levels
Label bar Controller
UITabBarController
Navigation Controller
(Tourism Information)
General View Controller
CityViewController
General View Controller
CityDetailViewController
General View Controller
(Food World)
MeiShiTianDi
 
Next we will create a label bar controller, as shown in the table above, which has two labels: "travel information" and "food world ". "Tourism information" has been completed. It manages two view controllers (cityViewController and CityDetailViewController). Therefore, the following describes the unfinished parts.
1. The project delegate AppDelegate declares that a tag controller is registered, initialized, and placed under the main Window
First, because the project name is still lvyou, you must declare a tag controller in project. h (lvyouAppDelegate. h:
[Plain]
// The original lvyouAppDelegate. h code starts.
# Import <UIKit/UIKit. h>
@ Interface lvyouAppDelegate: NSObject <UIApplicationDelegate> {
UIWindow * window;
UINavigationController * navController; // This line is newly added. The corresponding implementation file. m must have code!
UITabBarController * tabBarController; // declare a tag Controller
}
@ Property (nonatomic, retain) IBOutlet UIWindow * window;
@ End
// The original lvyouAppDelegate. h code is complete.
Second: in the project. in m (lvyouAppDelegate. m) initialize and put the Controller to be managed (in this example, there are two: one navigation controller and the other is the gourmet heaven View Controller) into its viewControllers array.
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {

TabBarController = [[UITabBarController alloc] init]; // initialize the label bar Controller
MeiShiTianDi * viewController = [[MeiShiTianDi alloc] init]; // initialize the Controller
ViewController. title = @ "Gourmet world ";
NavController = [[UINavigationController alloc] init]; // initialize the navigation Controller
// Add the Controller quantity matrix of the Controller in the label bar
// In this example, the two controllers navController viewController
TabBarController. viewControllers =
[NSArray arrayWithObjects: navController, viewController, nil];
[ViewController release]; // The memory is released because it is not the first view controller.
// Define the return button named "all cities. No target or action is required for this button.
// Because the system has implemented the return function. Style is the display Style
UIBarButtonItem * backButton =
[[UIBarButtonItem alloc] initWithTitle: @ "all cities"
Style: UIBarButtonItemStyleBordered
Target: nil action: nil];
// Set the first view controller to be displayed when the application is started. Here, cityViewContrl
CityViewController * cityViewContrl = [[cityViewController alloc] init];
CityViewContrl. title = @ "Travel Guide"; // set the title of the first View Controller
// Set the return button
CityViewContrl. navigationItem. backBarButtonItem = backButton;
[BackButton release];
// Push the first view controller to the stack.
[NavController pushViewController: cityViewContrl animated: NO];
// This is assumed to be the first view in the navigation bar, so the animation: NO should be animated.
[CityViewContrl release];
// Place the navigation (Label bar) Controller under the Window
// [Window addSubview: navController. view];
[Window addSubview: tabBarController. view]; // place the label bar Controller under the Window.

// Override point for customization after application launch.
[Window makeKeyAndVisible];
Return YES;
}

2. Create another (third) View Controller-food world: MeiShiTianDi
The method is the same as before:
> Select File> New File. In the New File window, select Cocoa Touch Classes and then UIViewController-subclass. In addition, select the selection box With XIB for user interface in the Options area. Name the file, and the View Controller name is MeiShiTianDi. At this time, the system has generated three files (*. h ,*. m ,*. xib), in the view of the New View Controller (MeiShiTianDi. (xib) add some buttons (add four buttons, named "Guangdong, Zhejiang, Sichuan, and Northeast ").
1) Add the Controller attribute of the label bar to lvyouAppDelegate. h -- (the previous step has been completed !)
2) In lvyouAppDelegate. in the m file, the-(BOOL) application :( UIApplication *) applicationdidfinishlaunchingwitexceptions :( NSDictionary *) launchOptions method creates a tag bar controller using code and initializes it, finally, the two controllers navController and viewController are placed on the label controller. -- (The previous step has been completed !)
[Plain]
// The original code of MeiShiTianDi. h starts.
# Import <UIKit/UIKit. h>
@ Interface MeiShiTianDi: UIViewController {
}
@ End
// The original code of MeiShiTianDi. m is finished.

// The original code of MeiShiTianDi. h starts.
# Import "MeiShiTianDi. h"
@ Implementation MeiShiTianDi

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(Void) viewDidLoad {
// UIImage * tabImage = [UIImage imageNamed: @ "shrimp.jpg"];
// Set the title and image of the View Controller on the tab bar
// The text is: Gourmet world. Image: UITabBarSystemItemBookmarks
UITabBarItem * item = [UITabBarItem alloc]
InitWithTitle: @ "food world"
InitWithTabBarSystemItem: UITabBarSystemItemBookmarks
// Image: tabImage
Tag: 0];
// In this example, the two lines of code using the image shrimp.jpg are also removed. You can also run it if you switch it.
Self. tabBarItem = item;
[Item release];
[Super viewDidLoad];
}
-(Void) didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[Super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

-(Void) viewDidUnload {
[Super viewDidUnload];
// Release any retained subviews of the main view.
// E.g. self. myOutlet = nil;
}
-(Void) dealloc {
[Super dealloc];
}
@ End
// The original code of MeiShiTianDi. m is finished.

3) set the title and image of the View Controller on the tab bar (UITabBarItem)
Each View Controller class has a UITabBarItem. This class allows you to set the title and image of the View Controller on the tab bar. Images and images are divided into two types: images and text (.png) and images and text provided by the system.
First, add the image to the project (in the resource folder)
Add the UITabBarItem code to the viewDidLoad method of cityViewController. m. Use your own image here. The image name is GoldenGateBridge.png.
[Plain]
-(Void) viewDidLoad {
// Create a style button with a border. After pressing it, call the discount method on the View Controller.
UIBarButtonItem * discountButton = [UIBarButtonItem alloc]
InitWithTitle: @ "discount Info" style: UIBarButtonItemStyleBordered
Target: self action: @ selector (discount :)];
Self. navigationItem. leftBarButtonItem = discountButton; // set the button to the left
[DiscountButton release]; // releases the memory.
// Set the title and image of the View Controller on the tab bar
// The text is: travel guide. Image: GoldenGateBridge.png
UITabBarItem * item = [UITabBarItem alloc]
InitWithTitle: @ "Travel Guide"
// InitWithTabBarSystemItem: UITabBarSystemItemBookmarks
Image: [UIImage imageNamed: @ "GoldenGateBridge.png"]
Tag: 0];
Self. tabBarItem = item;
[Item release];

[Super viewDidLoad];
}

Add UITabBarItem code to MeiShiTianDi. in the viewDidLoad method of m, the image provided by the system is used here. The image name is UITabBarSystemItemBookmarks. Of course, this example also commented out the two lines of code using the user's own image. You can also run it If you exchange it.
[Plain]
-(Void) viewDidLoad {
// UIImage * tabImage = [UIImage imageNamed: @ "shrimp.jpg"];
// Set the title and image of the View Controller on the tab bar
// The text is: Gourmet world. Image: UITabBarSystemItemBookmarks
UITabBarItem * item = [UITabBarItem alloc]
InitWithTitle: @ "food world"
InitWithTabBarSystemItem: UITabBarSystemItemBookmarks
// Image: tabImage
Tag: 0];
// In this example, the two lines of code using the user's image shrimp.jpg are also removed. You can also run it if you switch it.
Self. tabBarItem = item;
[Item release];
[Super viewDidLoad];
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.