IOS開發UI篇-NavigationController的控制器之間的跳轉

來源:互聯網
上載者:User

標籤:

 

一、如下

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的控制器之間的跳轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.