#import "AppDelegate.h"
#import "OneViewController.h"
#import "DetailViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//初始化OneViewController
OneViewController *oneVC = [[OneViewController alloc]init];
//初始化導航控制器,併爲導航控制器添加根視圖
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:oneVC];
//如果有導航控制器,就將導航控制器設置爲window的根視圖控制器
self.window.rootViewController = navC;
//設置導航條的顏色 通欄
navC.navigationBar.barTintColor = [UIColor orangeColor];
//設置導航條上所有按鈕的顏色
navC.navigationBar.tintColor = [UIColor redColor];
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
return YES;
}
#import "OneViewController.h"
#import "DetailViewController.h"
#import "TwoViewController.h"
@interface OneViewController ()
@end
@implementation OneViewController
- (void)viewDidLoad {
[super viewDidLoad];
//設置標題
// self.navigationItem.title = @"我是導航條";
//可以自定義標題區域顯示的內容或者控件
//分段控制器
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"One",@"Two"]];
//系統給保護措施了。所以位置無論怎麼變,總是顯示在導航條的最中間
segment.frame = CGRectMake(0, 0, 200, 30);
//顯示
self.navigationItem.titleView =segment;
//添加左邊按鈕(初始化)
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(myAddAction:)];
//將初始化好的左側按鈕賦給navigationITem的左側按鈕
self.navigationItem.leftBarButtonItem = leftBarButton;
//添加右側按鈕(初始化) //style:兩個枚舉值,一個粗體,一個細的
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@"HanOBa" style:UIBarButtonItemStyleDone target:self action:@selector(myAddAction1:)];
//將初始化好的右側按鈕賦給導航條的右側按鈕
self.navigationItem.rightBarButtonItem = rightBarButton;
//此代碼是讓導航器上左右可以加多個控制器,一組按鈕(是以數組的方式存在)
// self.navigationItem.rightBarButtonItems =@[leftBarButton,rightBarButton];
}
//點擊跳轉到詳情界面
-(void)myAddAction:(UIBarButtonItem *)sender{
//初始化將要跳轉到得視圖控制器
DetailViewController *detailVC = [[DetailViewController alloc]init];
//將要跳轉到的視圖控制器入棧(讓視圖控制器受導航控制器管理)
//<#(UIViewController *)#>是將要入棧的控制器(將要顯示的控制器)
//animated:<#(BOOL)#> 視圖切換的時候是否需要動畫
[self.navigationController pushViewController:detailVC animated:YES];
//目前受導航控制器管理的視圖控制器,入棧實際上就是將一個視圖控制器加到導航控制器的viewControllers數組中。目前所顯示的視圖控制器肯定是位於棧頂,也可以說是viewControllers數組的最後一個元素就是位於棧頂的視圖控制器
NSLog(@"被管理的---%@",self.navigationController.viewControllers);
//位於棧頂的視圖控制器
NSLog(@"棧頂---%@",self.navigationController.topViewController);
//目前正在顯示的視圖控制器
NSLog(@"正在顯示---%@",self.navigationController.visibleViewController);
}
//HanOBa按鈕的跳轉方法
-(void)myAddAction1:(UIBarButtonItem *)sender{
TwoViewController *twoVC = [[TwoViewController alloc]init];
[self.navigationController pushViewController:twoVC 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 "DetailViewController.h"
#import "TwoViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"詳情界面";
//自定義一個左側按鈕,覆蓋系統提供的返回按鈕
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
self.navigationItem.leftBarButtonItem = leftButton;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"Happy" style:UIBarButtonItemStylePlain target:self action:@selector(happyAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
}
//返回上級界面
-(void)backAction:(UINavigationController *)sender{
// [self.navigationController popViewControllerAnimated:YES];
NSLog(@"被管理者---%@",self.navigationController.viewControllers);
//返回到導航控制器的根視圖控制器(也就是初始化的時候OneViewController)
//這句和上句二選一,否則出棧兩次
[self.navigationController popToRootViewControllerAnimated:YES];
//返回指定的視圖控制器
//這裏的返回某個界面很複雜,我說不清楚。
[self.navigationController popToViewController:self.navigationController.viewControllers.firstObject animated:YES];
}
-(void)happyAction:(UINavigationController *)sender{
TwoViewController *twoVC = [[TwoViewController alloc]init];
[self.navigationController pushViewController:twoVC 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 "TwoViewController.h"
@interface TwoViewController ()
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
//給第三個頁面的導航條加一個標題
self.navigationItem.title = @"噠噠噠";
//創建一個右按鈕
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"返回HanOBa" style:UIBarButtonItemStyleDone target:self action:@selector(dadadaAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
}
//回調方法
-(void)dadadaAction:(UINavigationController *)sender{
//[self.navigationController popToViewController:self.navigationController.viewControllers.firstObject animated:YES];
//這句和上面那句意義一樣,不過這句是用下標法返回第i個視圖的,上面的只能找到第一個和最後一個視圖。
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] 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