导航控制器下自定义按钮完成界面跳转

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容