标签:
1 #import "AppDelegate.h" 2 #import "RootViewController.h" 3 @interface AppDelegate () 4 5 @end 6 7 @implementation AppDelegate 8 9 10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 11 // Override point for customization after application launch. 12 #pragma mark - UINagationController的创建 13 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 14 self.window.backgroundColor = [UIColor greenColor]; 15 [self.window makeKeyAndVisible]; 16 //1.创建一个根试图控制器 17 RootViewController *rootVc = [[RootViewController alloc] init]; 18 //2.创建导航控制器,将rootVC作为其根视图控制器,初始化必须有一个根视图控制器 19 UINavigationController *naVc = [[UINavigationController alloc] initWithRootViewController:rootVc]; 20 //3.将naVC作为window的根视图控制器 21 self.window.rootViewController = naVc; 22 naVc.delegate = self; 23 //NSLog(@"%@",naVc); 24 25 return YES; 26 } 27 #pragma mark - UINavigationControllerDelegate 代理方法 28 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 29 { 30 NSLog(@"%s",__FUNCTION__); 31 //navigationConroller的几个属性 32 NSLog(@"view controllers = %@",navigationController.viewControllers); 33 NSLog(@"top viweController = %@",navigationController.topViewController); 34 NSLog(@"visiableController = %@",navigationController.visibleViewController); 35 } 36 - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ 37 38 }
2.
1 #import "RootViewController.h" 2 #import "SencondViewController.h" 3 @interface RootViewController () 4 5 @end 6 7 @implementation RootViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 // Do any additional setup after loading the view. 12 self.view.backgroundColor = [UIColor redColor]; 13 #pragma mark - UINavigationBar(导航栏)的设置 14 /* 15 设置导航栏的样式barStyle和透明度translucent 16 */ 17 // NSLog(@"%@",self.navigationController); 18 // //导航栏默认的颜色是白色半透明状态 19 // self.navigationController.navigationBar.barStyle = UIBarStyleDefault; 20 // self.navigationController.navigationBar.translucent = YES; 21 // //第二种 白色不透明 22 // self.navigationController.navigationBar.barStyle = 0; 23 // self.navigationController.navigationBar.translucent = NO; 24 // //第三种 黑色,透明 25 // self.navigationController.navigationBar.barStyle = UIBarStyleBlack; 26 // self.navigationController.navigationBar.translucent = YES; 27 // //第四种 黑色, 不透明 28 // self.navigationController.navigationBar.barStyle = UIBarStyleBlack; 29 // self.navigationController.navigationBar.translucent = NO; 30 /* 31 设置导航栏的颜色:barTintColor 32 注意;barTintColor是iOS7以后的新特性,此修改不了导航栏的透明度,如果想修改导航栏的透明度,使用backgroundcolor 33 34 35 */ 36 //self.navigationController.navigationBar.barTintColor = [UIColor brownColor]; 37 self.navigationController.navigationBar.backgroundColor = [UIColor brownColor]; 38 /* 39 tintColor设置导航栏控件的颜色,默认的是蓝色 40 */ 41 self.navigationController.navigationBar.tintColor = [UIColor blueColor]; 42 /* 43 设置导航栏的背景图片:导航栏的竖屏高度是44;横屏的是36 44 像素图片:64,44和小于44的(要分清是否是一倍像素的) 45 */ 46 //一倍像素为:64 = 导航栏 + 状态栏 47 // UIImage *image = [UIImage imageNamed:@"NavBar_64"]; 48 // [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 49 // //一倍像素为:44 = 导航栏 50 // [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_44"] forBarMetrics:UIBarMetricsDefault]; 51 // //一倍像素为:小于44,这是不被允许的情况 52 // [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_30"] forBarMetrics:UIBarMetricsDefault]; 53 54 #pragma mark - 导航栏内容的设置UINavigationItem 55 //1.在工程里有且仅有一个UINavigationController,导航栏UINavigationBar也是有且仅有一个 56 //2.导航栏的所有内容基本上都在UINavigationItem 57 //3.每一个视图控制器都有一个UINavigationItem 58 59 60 //设置UINavigationItem的四种方式 61 //第一种方式:系统默认的 62 // UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftBarButtonAction)]; 63 // self.navigationItem .leftBarButtonItem = leftItem; 64 65 //第二种方式:使用title初始化的方法 66 // UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction)]; 67 // self.navigationItem.leftBarButtonItem = leftItem; 68 /* 69 //第三种方式:使用image初始化[此时图片没有保持原图片样式,因为默认是系统原模板样式,此时要实现效果需要对图片进行渲染] 70 UIImage *image1 = [UIImage imageNamed:@"NavBtnLeft"]; 71 //渲染之后,使用新的UIImage对象去接收 72 UIImage *image2 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 73 UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction)]; 74 self.navigationItem.rightBarButtonItem = rightItem; 75 //第四种方式:使用视图进行初始化 76 UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd]; 77 UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; 78 self.navigationItem.leftBarButtonItem = leftItem; 79 80 */ 81 //使用多个item进行赋值 82 UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(leftBarButtonAction)]; 83 UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(leftBarButtonAction)]; 84 self.navigationItem.leftBarButtonItems = @[item,item2]; 85 86 //使用多个item进行赋值 87 UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(leftBarButtonAction)]; 88 UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(leftBarButtonAction)]; 89 self.navigationItem.rightBarButtonItems = @[item3, item4]; 90 91 92 93 //中间的一块title和titleView 94 //self.title = @"首页"; 95 // self.navigationItem.title = @"首页"; 96 97 //使用View初始化titleView 98 self.navigationItem.titleView = [[UISegmentedControl alloc] initWithItems:@[@"消息",@"电话"]]; 99 //隐藏导航栏,默认的为NO 100 //self.navigationController.navigationBarHidden = YES; 101 102 //设置相关Controller的侧滑手势,navigationController作为根视图控制器才会有侧滑效果,默认为开,可以设置进行关闭 103 self.navigationController.interactivePopGestureRecognizer.enabled = NO; 104 105 #pragma mark - 页面跳转 106 //创建页面跳转的button 107 UIButton *pushNextControllerBtn = [UIButton buttonWithType:UIButtonTypeSystem]; 108 pushNextControllerBtn.frame = CGRectMake(100, 100, 100, 100); 109 [pushNextControllerBtn setTitle:@"push" forState:UIControlStateNormal]; 110 [pushNextControllerBtn addTarget:self action:@selector(pushButtonAction) forControlEvents:UIControlEventTouchUpInside]; 111 [self.view addSubview:pushNextControllerBtn]; 112 113 #pragma mark - 模态视图 114 UIButton *modelVVBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 115 modelVVBtn.frame = CGRectMake(100, 300, 100, 100); 116 [modelVVBtn setTitle:@"model" forState:UIControlStateNormal]; 117 [modelVVBtn addTarget:self action:@selector(modelViewControllerAction) forControlEvents:UIControlEventTouchUpInside]; 118 [self.view addSubview:modelVVBtn]; 119 120 121 122 } 123 #pragma mark - 模态出下一个界面 124 -(void)modelViewControllerAction 125 { 126 //1.创建一个需要模态的下一个Controller的对象 127 SencondViewController *secondVC = [[SencondViewController alloc] init]; 128 129 //2.设置转场动画 130 secondVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 131 132 //3.模态到下一个界面 133 //参数1:模态将要出现的controller 参数2:动画 参数3:模态完成需要处理的事件 134 [self presentViewController:secondVC animated:YES completion:^{ 135 136 }]; 137 } 138 #pragma mark - pushButton事件 139 -(void)pushButtonAction 140 { 141 //创建要跳转的controller 142 SencondViewController *secondVC = [[SencondViewController alloc] init]; 143 //利用导航控制器去退出下一个界面 144 [self.navigationController pushViewController:secondVC animated:NO]; 145 146 } 147 #pragma mark - 左侧button的方法 148 - (void)leftBarButtonAction 149 { 150 151 152 }
1 #import "SencondViewController.h" 2 #import "RootViewController.h" 3 #import "ThirdViewController.h" 4 @interface SencondViewController () 5 6 @end 7 8 @implementation SencondViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 // Do any additional setup after loading the view. 13 //注意了:在要推出的界面中,需要修改视图的背景颜色,用于解决跳转界面形似卡顿的一个现象,增加用户体验 14 self.view.backgroundColor = [UIColor blueColor]; 15 16 //创建返回按钮 17 UIButton *popViewController = [UIButton buttonWithType:UIButtonTypeSystem]; 18 popViewController.frame = CGRectMake(100, 100, 100, 100); 19 [popViewController setTitle:@"返回" forState:UIControlStateNormal]; 20 [popViewController addTarget:self action:@selector(popVCAction) forControlEvents:UIControlEventTouchUpInside]; 21 [self.view addSubview:popViewController]; 22 23 //创建PUSH按钮 24 UIButton *pushViewController = [UIButton buttonWithType:UIButtonTypeSystem]; 25 pushViewController.frame = CGRectMake(100, 300, 100, 100); 26 [pushViewController setTitle:@"push" forState:UIControlStateNormal]; 27 [pushViewController addTarget:self action:@selector(pushViewAction) forControlEvents:UIControlEventTouchUpInside]; 28 [self.view addSubview:pushViewController]; 29 //创建一个模态返回的按钮 30 UIButton *dismissViewController = [UIButton buttonWithType:UIButtonTypeSystem]; 31 dismissViewController.frame = CGRectMake(100, 500, 100, 100); 32 [dismissViewController setTitle:@"push" forState:UIControlStateNormal]; 33 [dismissViewController addTarget:self action:@selector(dismissViewAction) forControlEvents:UIControlEventTouchUpInside]; 34 [self.view addSubview:dismissViewController]; 35 36 } 37 //模态返回上一层级 38 -(void)dismissViewAction 39 { 40 [self dismissViewControllerAnimated:YES completion:^{ 41 42 }]; 43 //push适用于有层级关系一系列视图的跳转,它有层次递进关系,模态仅用于单独功能模块的跳转,不牵扯到主业务流程(登录,注册,拍照,相机) 44 } 45 //返回上一级页面的响应方法 46 -(void) popVCAction 47 { 48 // //1.返回根视图控制器 49 // [self.navigationController popToRootViewControllerAnimated:YES]; 50 // //2.返回上一层 51 // [self.navigationController popViewControllerAnimated:YES]; 52 //3.返回到指定的层级 [新手易犯的错,返回指定的Controller不应该创建,应该去桟管理的视图中寻找] 53 //通过打印当前视图控制器所控制的视图,可以找到相应视图的下标 54 NSLog(@"%@",self.navigationController.viewControllers); 55 RootViewController *rootVC = [self.navigationController.viewControllers objectAtIndex:0]; 56 [self.navigationController popToViewController:rootVC animated:YES]; 57 } 58 - (void)didReceiveMemoryWarning { 59 [super didReceiveMemoryWarning]; 60 // Dispose of any resources that can be recreated. 61 } 62 // 推出下一层页面的方法 63 -(void)pushViewAction 64 { 65 ThirdViewController *thirdView = [[ThirdViewController alloc] init]; 66 thirdView.view.backgroundColor = [ UIColor magentaColor]; 67 UIButton *popViewControllerBtn = [UIButton buttonWithType:UIButtonTypeSystem]; 68 [popViewControllerBtn setTitle:@"pop" forState:UIControlStateNormal]; 69 popViewControllerBtn.frame = CGRectMake(100, 100, 100, 100); 70 [popViewControllerBtn addTarget:self action:@selector(popViewAction) forControlEvents:UIControlEventTouchUpInside]; 71 [thirdView.view addSubview:popViewControllerBtn]; 72 [self.navigationController pushViewController:thirdView animated:NO]; 73 74 } 75 -(void)popViewAction 76 { 77 NSLog(@"viewControllers = %@",self.navigationController.viewControllers); 78 UIViewController *viewController = [self.navigationController.viewControllers objectAtIndex:1]; 79 [self.navigationController popToViewController:viewController animated:NO]; 80 }
标签:
原文地址:http://www.cnblogs.com/DevinSMR/p/5215211.html