標籤:
一、如下
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的控制器之間的跳轉