The Iphone uses UINavigationController to implement navigation between two pages (on)

Source: Internet
Author: User

Requirements: 1. Starting from an empty project template based on Xcode
2. data can be transmitted between two pages.
3. Add the system button and the left button on the navigation control bar.
Implementation Method Analysis
1. According to the MVC mode, iphone programs are composed of view-model-controller. Therefore, two View controllers and Their managed views are displayed on the two pages. Therefore:
The first page is defined as cityViewController.
The second page is defined as CityDetailViewController.
The navigation controller switches between the two view controllers! Project name: lvyou
2. Development steps:
Step 1: Create a project. Project name: lvyou, Windows-based Application)
Step 2: Create the cityViewController View Controller.
> 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. In this example, the View Controller name is cityViewController (the class name must start with an uppercase letter ). Be sure to create. m and. H files and add them to the project. At this time, the system has generated three files (*. h, *. m, *. xib) and actually implemented four tasks:
1) The system creates a View Controller (named File 'sowner object) for you and the class name is cityViewController.
2) The system also creates a view for you (open on *. xib)
3) in the view Controller class, the system also defines an IBOulet variable view for you.
4) This variable (the IBOulet variable view of the output port) has been associated with the view
Step 3: Create the CityDetailViewController View Controller in the same way.
> 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 of the file. The View Controller name is CityDetailViewController. At this time, the system has generated three files (*. h, *. m, *. xib), and has actually implemented the above four tasks.
Step 4: add (Register) a navigation controller.
(Registering cityViewController is the first controller to display views.) You can only use the code method! (Of course, if you select "navigation-based application" when creating a project ". A Root View Controller named RootViewController is automatically created for you, and some code is generated automatically, such as placing the view of the navigation controller in the window view)
Method: declare (ADD) A UINavigationController Class Object on 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.

// The original lvyouAppDelegate. m code starts.
# Import "lvyouAppDelegate. h"
# Import "cityViewController. h" // Add
# Import "MeiShiTianDi. h"
@ Implementation lvyouAppDelegate
@ Synthesize window;
# Pragma mark-
# Pragma mark Application lifecycle

-(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
TabBarController. viewControllers =
[NSArray arrayWithObjects: navController, viewController, nil];

[ViewController release];
// 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];

// Override point for customization after application launch.
[Window makeKeyAndVisible];
Return YES;
}
-(Void) applicationWillResignActive :( UIApplication *) application {

}
-(Void) applicationDidEnterBackground :( UIApplication *) application {

}
-(Void) applicationWillEnterForeground :( UIApplication *) application {

}
-(Void) applicationDidBecomeActive :( UIApplication *) application {

}
-(Void) applicationWillTerminate :( UIApplication *) application {

}
# Pragma mark-
# Pragma mark Memory management
-(Void) applicationDidReceiveMemoryWarning :( UIApplication *) application {

}
-(Void) dealloc {
[TabBarController release]; // releases the memory.
[NavController release];
[Window release];
[Super dealloc];
}
@ End
// The original lvyouAppDelegate. m code is complete.

Step 5: Design and improve the cityViewController View Controller.
First, add a button on cityViewController. xib and change the button title to "Beijing ". Declare a method selectCity in cityViewController. h. Connect the button to the method!
[Plain]
// The original code of cityViewController. h starts.
# Import <UIKit/UIKit. h>
@ Interface cityViewController: UIViewController {
}
-(IBAction) selectCity: (id) sender; // customize a method. Click "Beijing" to go to the next page.
@ End
// The original cityViewController. h code is complete.

// The original cityViewController. m code starts.
# Import "cityViewController. h"
# Import "CityDetailViewController. h" // newly added, declare registration of CityDetailViewController

@ Implementation cityViewController // define attributes
// Implement a custom method (function)
-(IBAction) selectCity: (id) sender {
CityDetailViewController * cityDetailContrl
= [[CityDetailViewController alloc] init]; // initialize CityDetailViewController
CityDetailContrl. title = @ "Beijing Welcomes You"; // sets the title of the second view controller.
CityDetailContrl. city = @ "Beijing"; // sets the data to be uploaded to the next controller-Beijing
// Push the second view controller into the stack
[Self. navigationController pushViewController: cityDetailContrl animated: YES];
[CityDetailContrl release]; // releases the memory.
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(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];
}


-(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 cityViewController. m code is complete.

Step 6: Design and improve the CityDetailViewController View Controller.
First in CityDetailViewController. add two labels on the xib instance. The first Label changes the text to "Introduction Information of Beijing" (in actual application, the introduction information of the city should be read from the model class ), the second Label is used to modify the text "city name". Because a data (city name) is transferred from the previous page, it is read and displayed on the second page. In CityDetailViewController. h declares an IBOutlet (named cityName), which is used to associate the "city name" Label created on the View. In addition, create a property "city" to receive data (city name) transmitted from the previous page ).

[Plain]
// The original CityDetailViewController. h code starts.
# Import <UIKit/UIKit. h>
@ Interface CityDetailViewController: UIViewController {
IBOutlet UILabel * cityName; // The second Label Creation Interface IBOutlet
NSString * city; // create an attribute
}
@ Property (copy) NSString * city; // create an Attribute set/get Method
@ End
// The original CityDetailViewController. h code is complete.

// The original CityDetailViewController. m code starts.
# Import "CityDetailViewController. h"
@ Implementation CityDetailViewController // implement the Controller class
@ Synthesize city; // attribute city variable

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(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];
}

-(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 CityDetailViewController. m code is complete.

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.