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
网友评论