Create a project:
Create a simple view interface in the Model, while the ViewController class is a table interface.
Create a table:
Step 1:
Step 2:
Note: Select UITableViewController for Subclass.
Next, click create.
=- =-=
In the ViewController interface, create navigation and add it
/* AppDelegate. h */# import <UIKit/UIKit. h> @ class ViewController; @ interface AppDelegate: UIResponder <UIApplicationDelegate> @ property (strong, nonatomic) UIWindow * window; // Add a navigation controller @ property (strong, nonatomic) UINavigationController * iNav; // Add the ViewController interface to add the navigation to this interface: @ property (strong, nonatomic) ViewController * viewController; @ end
/* AppDelegate. m */# import "AppDelegate. h "# import" ViewController. h "@ implementation AppDelegate @ synthesize window = _ window; // --------------- @ synthesize iNav = _ iNav; @ synthesize viewController = _ viewController; // ------------------ (void) dealloc {[_ iNav release]; [_ viewController release]; [_ window release]; [super dealloc];}-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease]; // Override point for customization after application launch. self. window. backgroundColor = [UIColor whiteColor]; [self. window makeKeyAndVisible];/* --------------- added content ---------------- */self. viewController = [[ViewController alloc] initWithNibName: @ "ViewController" bundle: nil]; // The page navigation of initWithRootViewController based on (that is, the page to which the navigation is added) self. iNav = [[UINavigationController alloc] initWithRootViewController: self. viewController]; // Add to window [self. window addSubview: self. iNav. view]; // or the following method // self. window. rootViewController = self. iNav; return YES ;}
Write a title for the navigation bar in the ViewController. m class.
In the-(void) viewDidLoad method, add:
Self. title = @ "this is a navigation ";
Now, you can add a navigation bar in this interface.
=- =-=
However, the navigation function is not shown here. Therefore, we perform the following operations:
This interface is a table interface. You can add content in this interface and switch it to another interface to implement navigation: return to the first interface through navigation
Or in the ViewController. m class:
In the-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section method
Return cell; Add the following code before:
// Input cell. textLabel. text = @ "helloworld"; // obtain the index value of each row. int num = indexPath. row;
There are two methods in this class: set the number of regions and the number of rows in each region.
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {// Return the number of sections. // return Return 1;}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {// return the number of rows in the section. return 2; // return the rows in a region}
At this time, the interface is:
Click helloworld to enter the next interface, that is, the UiViewController. xib interface, which contains a label control.
You can switch the interface in the ViewController. m class:
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {UiViewController * viewController = [[UiViewController alloc] initWithNibName: @ "UIViewController" bundle: nil]; // navigationController is an attribute inherited from UIViewController. It is used to call its stack pressure method to implement interface switching/* @ property (nonatomic, readonly, retain) UINavigationController * navigationController; // If this view controller has been pushed onto a navigation controller, return it. */[self. navigationController pushViewController: viewController animated: YES]; // Navigation logic may go here. create and push another view controller. /* <# DetailViewController #> * detailViewController = [[<# DetailViewController #> alloc] initWithNibName: @ "<# Nib name #>" bundle: nil]; //... // Pass the selected object to the new view controller. [self. navigationController pushViewController: detailViewController animated: YES]; [detailViewController release]; */}
Note: Do not forget to add the header file:
# Import "uiviewcontroller. H"
=- =-=
Now, the navigation function is realized.
Click helloworld to go to the next page.
On the second page, you can see that there is a return button in the navigation bar and you can click it to return to the first page.