import "AppDelegate.h"
import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];// 初始化RootVC(rootVC為navC的根視圖控制器,也就是首個被navC管理的視圖控制器)
RootViewController *rootVC = [[RootViewController alloc]init];
// 初始化navC (初始化方法使用的是可以給navC設置根視圖控制器)
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:rootVC];// 設置navigationBar的tintColor(前端渲染的顏色) 其實是navigationBar上面返回按鈕的顏色
navC.navigationBar.tintColor = [UIColor redColor];
// 改變navigationBar的背景顏色
// navC.navigationBar.backgroundColor = [UIColor yellowColor];
// 改變navigationBar的透明狀態(默認為半透明)
navC.navigationBar.translucent = NO;
// 設置背景圖片
// UIBarMetricsDefault 橫、豎屏狀態下的背景圖片
// UIBarMetricsCompact 橫屏狀態下的背景圖片
[navC.navigationBar setBackgroundImage:[UIImage imageNamed:@"1.png"] forBarMetrics:UIBarMetricsCompact];
// 設置navigationBar的樣式 (系統提供的只有兩種,淺白色和黑色)
navC.navigationBar.barStyle = UIBarStyleBlack;// 為window設置根視圖控制器
[self.window setRootViewController:navC];return YES;
}
import "RootViewController.h"
import "SecondViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
// 視圖將要出現的時候
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"Root___%@",self.navigationController.viewControllers);
}
-
(void)viewDidLoad {
[super viewDidLoad];// 設置導航欄標題
self.navigationItem.title = @"根視圖控制器";
// 設置titleView(標題視圖)
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 44)];
titleLabel.text = @"titleView";
titleLabel.backgroundColor = [UIColor greenColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = titleLabel;
// 為了下個界面的返回按鈕處沒有文字,在已經使用titleView的情況下,可以將標題設置為空字符串
self.navigationItem.title = @"";
// 設置navigationBar的右按鈕
// <#(UIBarButtonSystemItem)#>:當前按鈕的系統樣式UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(rightBarBtnAction:)];
// 設置右側按鈕
self.navigationItem.rightBarButtonItem = rightBtnItem;UIButton *pushBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[pushBtn setTitle:@"push" forState:UIControlStateNormal];
pushBtn.frame = CGRectMake(100, 100, 100, 100);
[pushBtn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pushBtn];
}
// 導航欄右側按鈕回調方法
-(void)rightBarBtnAction:(UIBarButtonItem*)sender{
// push到下個界面
SecondViewController *secondVC = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secondVC animated:YES];
}
// 按鈕的回調方法,進入下一個界面
-(void)pushAction:(UIButton*)sender{
// 打印viewController,看一下當前被navC所管理的視圖控制器們
NSArray *viewCs = self.navigationController.viewControllers;
NSLog(@"%@",viewCs);
// 進入到下一個界面(為導航控制器新添加一個要被管理的視圖控制器)
SecondViewController *secondVC = [[SecondViewController alloc]init];
// 推送到下一個界面
/**
* pushViewController:將要顯示的視圖控制器
animated:切換中是否需要動畫
push的操作,就是將所要被管理的視圖控制器入棧,實際執行的動作就是:[self.navigationController.viewControllers addObjects:secondVC]在此處secondVC就會位于棧頂
viewControllers:該屬性的返回類型為NSArray,但是我們一定得記得,viewControllers實際的類型是NSMutableArray。這里體現了面向對象多態的特性
*/
[self.navigationController pushViewController:secondVC animated:YES];
NSLog(@"push*****%@",self.navigationController.viewControllers);
// 得到當前位于棧頂的視圖控制器 viewControllers這個數組的最后一個元素,一般都在棧頂
NSLog(@"top____%@",self.navigationController.topViewController);
// 得到當前正在顯示的視圖控制器
NSLog(@"visible~~~~~~%@",self.navigationController.visibleViewController);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
import "SecondViewController.h"
import "ThirdViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
-
(void)viewDidLoad {
[super viewDidLoad];// 設置左邊按鈕
UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(leftBackBtnAction:)];
self.navigationItem.leftBarButtonItem = leftBtnItem;UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
leftBtn.frame = CGRectMake(0, 0, 50, 50);
[leftBtn setTitle:@"button" forState:UIControlStateNormal];
// 當我們使用自定義視圖(UIView及其子類)作為導航條按鈕的時候,需要使用此初始化方法
UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];// 定義一組左側按鈕
self.navigationItem.leftBarButtonItems = @[barItem,leftBtnItem];UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[backBtn setFrame:CGRectMake(100, 100, 100, 100)];
[backBtn setTitle:@"popBtn"
forState:UIControlStateNormal];
[backBtn addTarget:self
action:@selector(popAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];
UIButton *thirdBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[thirdBtn setFrame:CGRectMake(100, 200, 100, 100)];
[thirdBtn setTitle:@"thirdBtn"
forState:UIControlStateNormal];
[thirdBtn addTarget:self
action:@selector(thirdAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:thirdBtn];
}
// 左邊按鈕的回調方法
-(void)leftBackBtnAction:(UIBarButtonItem*)sender{
[self.navigationController popToRootViewControllerAnimated:YES];
}
// 返回上級界面(出棧)
-(void)popAction:(UIButton*)sender{
// 出棧 實際所進行的操作是:[self.navigationController.viewControllers removeObject:self]
// 更準確的寫法:[self.navigationController.viewControllers removeObject:self.navigationController.viewControllers.lastObject]
// 在出棧之前打印當前navC過管理的視圖控制器
NSLog(@"pre ***%@",self.navigationController.viewControllers);
// [self.navigationController popViewControllerAnimated:YES];
// 返回到指定視圖控制器
[self.navigationController popToViewController:self.navigationController.viewControllers.firstObject animated:YES];
// 出棧之后,再次打印當前navC所管理的視圖控制器
NSLog(@"pop ~~~%@",self.navigationController.viewControllers);
}
// 跳轉到thirdVC
-(void)thirdAction:(UIButton*)sender{
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
[self.navigationController pushViewController:thirdVC 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
import "ThirdViewController.h"
@interface ThirdViewController ()
@end
@implementation ThirdViewController
-
(void)viewDidLoad {
[super viewDidLoad];UIButton *thirdBackBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[thirdBackBtn setFrame:CGRectMake(100, 200, 200, 100)];
[thirdBackBtn setTitle:@"thirdBackRootBtn"
forState:UIControlStateNormal];
[thirdBackBtn addTarget:self
action:@selector(thirdBackRootAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:thirdBackBtn];
}
-(void)thirdBackRootAction:(UIButton*)sender{
// 返回根視圖控制器
NSLog(@"third___%@",self.navigationController.viewControllers);
[self.navigationController popToRootViewControllerAnimated:YES];
NSLog(@"thirdPop___%@",self.navigationController.viewControllers);
}
- (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