标签:
1> 第一个控制器的NavigationBar隐藏
2> 有按钮,可以跳转到下一个控制器,返回上一个控制器或者根控制器
思路:
1> 设置window的跟控制器为navigationController
2> 设置一个控制器的基类
主要代码实现如下:
AppDelegate.m
// // AppDelegate.m // navigation的简单实用(纯代码) // // Created by gxiangzi on 15/8/7. // Copyright (c) 2015年 itcast. All rights reserved. // #import "AppDelegate.h" #import "GXNavController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; // 创建一个导航控制器 GXNavController* navc = [[GXNavController alloc] init]; // 设置跟控制器为导航控制器 self.window.rootViewController = navc; [self.window makeKeyAndVisible]; return YES; } .....
导航控制器类:GXNavController.h
#import <UIKit/UIKit.h> @interface GXNavController : UINavigationController @end
导航控制器类:GXNavController.m
// // GXNavController.m // navigation的简单实用(纯代码) // // Created by gxiangzi on 15/8/7. // Copyright (c) 2015年 itcast. All rights reserved. // #import "GXNavController.h" #import "GXFirstController.h" @interface GXNavController () @end @implementation GXNavController // 重写父类的init方法,设置跟控制器 - (instancetype)init { GXFirstController* first = [[GXFirstController alloc] init]; first.view.backgroundColor = [UIColor whiteColor]; return [super initWithRootViewController:first]; } - (void)viewDidLoad { [super viewDidLoad]; // 第一个隐藏导航条 self.navigationBar.hidden = YES; } // 重写导航控制器的push方法,设置返回按钮为back - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { // 显示导航条 self.navigationBar.hidden = NO; [super pushViewController:viewController animated:animated]; } - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated { // 第一个隐藏导航条 self.navigationBar.hidden = YES; return [super popToRootViewControllerAnimated:animated]; } @end
基类:GXBaseViewController.h
#import <UIKit/UIKit.h> @interface GXBaseViewController : UIViewController @property (nonatomic,weak)UIButton *popBtn; @property (nonatomic,weak)UIButton *pushBtn; - (void)pop2Previous; - (void)push2Next; @end
基类:GXBaseViewController.m
// // GXBaseViewController.m // navigation的简单实用(纯代码) // // Created by gxiangzi on 15/8/7. // Copyright (c) 2015年 itcast. All rights reserved. // #import "GXBaseViewController.h" @interface GXBaseViewController () @end @implementation GXBaseViewController - (void)viewDidLoad { [super viewDidLoad]; // 创建返回的按钮 UIButton* popBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [popBtn setTitle:@"返回上一个控制器" forState:UIControlStateNormal]; [popBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; popBtn.backgroundColor = [UIColor grayColor]; popBtn.frame = CGRectMake(100, 100, 200, 50); self.popBtn = popBtn; // 创建Push到下一个控制器 UIButton* pushBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [pushBtn setTitle:@"push到下一个控制器" forState:UIControlStateNormal]; [pushBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; pushBtn.backgroundColor = [UIColor grayColor]; pushBtn.frame = CGRectMake(100, 300, 200, 50); self.pushBtn = pushBtn; // 注册监听事件 [self.popBtn addTarget:self action:@selector(pop2Previous) forControlEvents:UIControlEventTouchUpInside]; [self.pushBtn addTarget:self action:@selector(push2Next) forControlEvents:UIControlEventTouchUpInside]; }
#pragma mark - 子类重写 #pragma mark - 上一个 - (void)pop2Previous { [self.navigationController popViewControllerAnimated:YES]; } #pragma mark -下一个 - (void)push2Next { [self.navigationController pushViewController:nil animated:YES]; } @end
IOS开发UI篇-NavigationController的控制器之间的跳转
标签:
原文地址:http://www.cnblogs.com/gaox97329498/p/4711910.html