您的位置:首页 > 移动开发 > IOS开发

ios开发之导航控制器的原理

2016-04-14 00:00 573 查看
摘要: 导航控制器的原理
#import "AppDelegate.h"#import "FirstViewController.h"#import "SecondViewController.h"#import "ThirdViewController.h"#import "ForthViewController.h"@interface  AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch._window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];[_window setBackgroundColor:[UIColor whiteColor]];#pragma mark -导航控制器原理//=======================原理===================//1.导航控制器:是一个容器视图控制器,专门用来管理其他的视图控制器//导航控制器管理的视图控制器在导航控制器中的存储结构是栈结构;//2.导航控制器永远显示栈顶的那个视图视图控制器,//3.让一个导航控制器去管理其他视图控制器的方法://(1).将视图控制器作为导航控制器的根视图控制器,//(2).使用导航控制器去push出来的视图控制器,也是属于导航控制器的视图控制器#pragma mark -导航控制器的使用//=====================使用=============//创建一个视图控制器,用来作为导航控制器的根视图控制器FirstViewController *first =[[FirstViewController alloc]init];//如果一个视图控制器作为导航控制器的根视图控制器,那么这个视图控制器就是//导航控制器栈结构的容器中的一员(而且还是第一个添加的)//1.创建导航控制器对象//每个导航控制器必须至少管理一个视图,所以创建方法如下//UINavigationController : UIViewControllerUINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:first];//3.添加视图控制器//push一个视图控制器到导航控制器中//导航控制器用来将视图控制器添加到栈中的方法// SecondViewController *second = [[SecondViewController alloc]init];//[navigationController pushViewController:second animated:YES];// ThirdViewController *third//4.pop一个视图控制器就会将这个视图从导航控制器中移除,前提是这个视图控制器//已经在这个导航控制器中,导航控制器中的根视图控制器不可以移除//一般这个pop方法只能写在你想要移除的视图控制器中去调用;//2.显示在window上_window.rootViewController = navigationController;[_window makeKeyAndVisible];return YES;}@endForthViewController.m:#import "ForthViewController.h"#import "UIView+FJView.h"@interface  ForthViewController ()@end@implementation ForthViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor = [UIColor cyanColor];//添加一个按钮UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 30, 20)];[button setBackgroundColor:[UIColor redColor]];[ button addTarget:self action:@selector(onClicked:)forControlEvents:UIControlEventTouchUpInside];self.title = @"第四页";[self.view addSubview:button];}#pragma mark - 按钮点击- (void)onClicked:(UIButton *) btn{//1.将当前这个导航控制器最上层的视图控制器pop掉//回到上一层视图控制器//[self.navigationController popViewControllerAnimated:YES];//2.将当前导航控制器pop到根视图控制器// [self.navigationController popToRootViewControllerAnimated:YES];//拿到当前导航控制器管理的所有的视图控制器(导航控制器管理的视图控制器全是//导航控制器的子视图控制器)//子视图控制器数组中数组元素得顺序和视图的添加顺序一致;NSArray *chirlViewControllers = self.navigationController.childViewControllers;//添加转场动画;[self.view addTransitionAnimationWithDuration:1 animationType:FJ_pageUnCurl direction:FJ_LEFT];//使用转场动画和push方法的区别;一个放到导航控制器上一个只是纯展示;//3.pop到指定的视图控制器;(指定的视图控制器必须已经在导航控制器的栈结构中的对象)[self.navigationController popToViewController:chirlViewControllers[1] animated:YES];}@endThirdViewController.m#import "ThirdViewController.h"#import "ForthViewController.h"@interface ThirdViewController ()@end@implementation ThirdViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor = [UIColor blueColor];//添加一个按钮UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 30, 20)];[button setBackgroundColor:[UIColor redColor]];[ button addTarget:self action:@selector(onClicked:)forControlEvents:UIControlEventTouchUpInside];self.title = @"第三页";[self.view addSubview:button];}#pragma mark - 按钮点击- (void)onClicked:(UIButton *) btn{ForthViewController *forth = [[ForthViewController alloc]init];[self.navigationController pushViewController:forth animated:YES];}@endSecondViewController.m#import "SecondViewController.h"#import "ThirdViewController.h"@interface SecondViewController ()@end@implementation SecondViewController#pragma mark - 生命周期- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.UIButton *button  = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 40, 20)];button.backgroundColor = [UIColor cyanColor];[button addTarget:self action:@selector(onclicked)forControlEvents:UIControlEventTouchDown];[self.view addSubview:button];self.title = @"第二页";[self.view setBackgroundColor:[UIColor orangeColor]];}#pragma mark - 按钮点击-(void) onclicked{//pop回到上一个界面;//pop方法属于导航控制器;//pop当前这个视图控制器// - (UIViewController *)popViewControllerAnimated:(BOOL)animated;// [self.navigationController popViewControllerAnimated:YES];ThirdViewController *third = [[ThirdViewController alloc]init];[self.navigationController pushViewController:third animated:YES];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}@endFirstViewController.m#import "FirstViewController.h"#import "SecondViewController.h"@interface FirstViewController ()@end@implementation FirstViewController#pragma mark -生命周期- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor = [UIColor lightGrayColor];//添加一个按钮UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 30, 20)];[button setBackgroundColor:[UIColor redColor]];[ button addTarget:self action:@selector(onClicked:)forControlEvents:UIControlEventTouchUpInside];self.title = @"第一页";[self.view addSubview:button];}#pragma mark - 按钮点击- (void)onClicked:(UIButton *) btn{//创建一个需要push的视图控制器对象SecondViewController *second = [[SecondViewController alloc]init];//只有这个视图控制器添加到导航控制器上,才可以拿到导航控制器//使用当前这个导航控制器push一个新的视图控制器;[self.navigationController pushViewController:second animated:YES];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.}*/@end
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  导航控制器原理