导航控制器是UINavigationController作为根控制器,根试图控制器是根控制器控制的第一个试图,作为启动第一次加载的试图。而且只有在导航控制器中才有根试图控制器之称。所以根控制器与根试图控制器有本质的区别,程序可以从根试图控制器发起其它试图。
下面看看导航栏控制器的原理图:
导航控制器它们的试图是由一个栈结构来管理,这个和Android里面的Activity栈类似。
我们现在来看看导航栏控制器试图跳转的代码实现步骤:
1、创建一个Navigation-based Application的控制器类,由于导航栏控制器常常和表试图搭配用,所以在生成的根试图控制器默认继承UITableViewController。为了简单起见,我先让这个根试图控制器继承普通的试图控制器UIViewController。
UITabBarController是控件的视图控制器,可以作为根控制器,这里我将介绍标签栏控制器来实现视图之间的跳转。
2、点击RootViewController,删除默认表试图,创建新的普通视图,然后按下图连线,如下图
3、创建其它普通视图,在视图跳转的过程控制中,下面的代码分成连线的方式与代码方式注册前进按钮
运行结果如下图:
程序运行出现黄色试图——>红色试图——>绿色试图
代码:
RootViewController.h
#import <UIKit/UIKit.h> #import "GreenViewControl.h" #import "RedViewControl.h" @interface RootViewController : UIViewController { GreenViewControl * green; RedViewControl* red; } @property(nonatomic,retain) GreenViewControl* green; @property(nonatomic,retain) RedViewControl* red; -(IBAction)moveToNextView:(id)sender; @end
RootViewController.m
#import "RootViewController.h" @implementation RootViewController @synthesize red; @synthesize green; -(IBAction)moveToNextView:(id)sender { RedViewControl* redCV = [[RedViewControl alloc] initWithNibName:@"RedViewControl" bundle:nil]; self.red = redCV; [redCV release]; [self.navigationController pushViewController:self.red animated:YES]; } - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } /* // Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations. return (interfaceOrientation == UIInterfaceOrientationPortrait); } */ // Customize the number of sections in the table view. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 0; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell. return cell; } /* // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } */ /* // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. } } */ /* // Override to support rearranging the table view. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { } */ /* // Override to support conditional rearranging of the table view. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the item to be re-orderable. return YES; } */ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { /* <#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]; */ } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Relinquish ownership any cached data, images, etc that aren't in use. } - (void)viewDidUnload { [super viewDidUnload]; // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. // For example: self.myOutlet = nil; } - (void)dealloc { [red dealloc]; [green dealloc]; [super dealloc]; } @end
RedViewControl.h
#import <UIKit/UIKit.h> @interface RedViewControl : UIViewController { UIViewController* green; } @property(nonatomic,retain) UIViewController* green; -(IBAction)moveToNextView:(id)sender; @end
RedviewControl.m
#import "RedViewControl.h" @implementation RedViewControl @synthesize green; -(IBAction)moveToNextView:(id)sender { UIViewController* greenCV = [[UIViewController alloc] initWithNibName:@"GreenViewControl" bundle:nil]; self.green = greenCV; [self.navigationController pushViewController:self.green animated:YES]; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (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. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"绾㈣壊"; UIBarButtonItem* nextButton = [[UIBarButtonItem alloc] initWithTitle:@"鍓嶈繘" style:UIBarButtonItemStyleBordered target:self action:@selector(moveToNextView:)]; self.navigationItem.rightBarButtonItem = nextButton; [nextButton release]; // Do any additional setup after loading the view from its nib. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(void) dealloc { [green dealloc]; [super dealloc]; } @end
GreenViewControl.h
#import <UIKit/UIKit.h> @interface GreenViewControl : UIViewController @end
GreenViewControl.m
#import "GreenViewControl.h" @implementation GreenViewControl - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (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. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"缁胯壊"; // Do any additional setup after loading the view from its nib. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end