#import "AppDelegate.h"數組
#import "FirstViewController.h"app
#import "SecondViewController.h"ide
#import "ThirdViewController.h"動畫
#import "ForthViewController.h"url
@interface AppDelegate ()spa
@end .net
@implementation AppDelegate3d
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {code
// Override point for customization after application launch.orm
_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 : UIViewController
UINavigationController *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;
}
FirstViewController.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.
}
@end
SecondViewController.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.
}
@end
ThirdViewController.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];
}
@end
ForthViewController.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];
}
@end