博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS第12天(2,UINavigationController导航控制器)
阅读量:5092 次
发布时间:2019-06-13

本文共 6457 字,大约阅读时间需要 21 分钟。

****HMAppDelegate.m

@implementation HMAppDelegate- (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];        HMOneViewController *oneVc = [[HMOneViewController alloc] init];        UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:oneVc];            self.window.rootViewController = navVc;        [self.window makeKeyAndVisible];    return YES;}-(void)test1{    //1.创建一个导航控制器    //UINavigationController *navVc = [[UINavigationController alloc] init];        //2.设置导航控制器的子控制器    UIViewController *oneVc = [[UIViewController alloc] init];    oneVc.view.backgroundColor  = [UIColor grayColor];        UIViewController *twoVc = [[UIViewController alloc] init];    twoVc.view.backgroundColor = [UIColor purpleColor];        //method 1 : 添加导航控制器的子控制器    //[navVc pushViewController:oneVc animated:YES];    //[navVc pushViewController:twoVc animated:YES];        //method 2    //navVc.viewControllers = @[oneVc];            //method 3    //    [navVc addChildViewController:oneVc];    //    [navVc addChildViewController:twoVc];    //self.window.rootViewController = navVc;}

******HMOneViewController.m

#import "HMOneViewController.h"#import "HMTwoViewController.h"@interface HMOneViewController ()- (IBAction)jumpTwoVc:(id)sender;@end@implementation HMOneViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.    //NSLog(@"%@",self.navigationController.viewControllers);        //设置导航栏的内容    //设置标题    self.navigationItem.title = @"第一个控制器";        //设置导航栏左边的按钮    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];    self.navigationItem.leftBarButtonItem = leftBtn;        //设置下一个控制器的返回按钮    //当前控制的navigationItem里的返回按钮是决定下一个控制器的返回按钮    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}//跳到第二个控制器- (IBAction)jumpTwoVc:(id)sender {        HMTwoViewController *twoVc = [[HMTwoViewController alloc] init];        NSLog(@"%@",self.navigationController);    [self.navigationController pushViewController:twoVc animated:YES];}@end

*******HMTwoViewController.m

#import "HMTwoViewController.h"#import "HMThreeViewController.h"@interface HMTwoViewController ()- (IBAction)backOneVc:(id)sender;- (IBAction)jumpThreeVc;@end@implementation HMTwoViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.    //NSLog(@"%@",self.navigationController.viewControllers);        //设置标题    self.navigationItem.title = @"第二个控制器";        //设置返回按钮    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"BACK" style:UIBarButtonItemStylePlain target:nil action:nil];        //设置导航栏左边的按钮    //如果设置leftBarButtonItem ,之前那个返回无效    UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self  action:@selector(back)];    self.navigationItem.leftBarButtonItem = leftBtnItem;        //设置导航栏右边的按钮    UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:nil action:nil];    //self.navigationItem.rightBarButtonItem = search;        UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:nil action:nil];        self.navigationItem.rightBarButtonItems = @[search,refresh];    }//返回上一个控制器-(void)back{    [self.navigationController popViewControllerAnimated:YES];}- (IBAction)backOneVc:(id)sender {        [self.navigationController popViewControllerAnimated:YES];}- (IBAction)jumpThreeVc {        //跳到第三个控制器    HMThreeViewController *threeVc = [[HMThreeViewController alloc] init];    [self.navigationController pushViewController:threeVc animated:YES];}@end

********HMThreeViewController.m

#import "HMThreeViewController.h"@interface HMThreeViewController ()- (IBAction)backOneVc:(id)sender;@end@implementation HMThreeViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.        //栈的所有控制器    NSLog(@"%@",self.navigationController.viewControllers);    NSArray *array = [self.navigationController childViewControllers];    NSLog(@"%@",array);        //设置标题View    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];        self.navigationItem.titleView = btn;        //设置返回按钮btn_back_normal    UIButton  *redBtn = [UIButton buttonWithType:UIButtonTypeCustom];    redBtn.bounds =  CGRectMake(0, 0, 46, 31);    [redBtn setBackgroundImage:[UIImage imageNamed:@"btn_back_normal"] forState:UIControlStateNormal];        [redBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];        UIBarButtonItem *redBackBtnItem = [[UIBarButtonItem alloc] initWithCustomView:redBtn];        self.navigationItem.leftBarButtonItem = redBackBtnItem;    }//返回上一个控制咕噜-(void)back{    [self.navigationController popViewControllerAnimated:YES];}- (IBAction)backOneVc:(id)sender {        //返回第一个控制器    //[self.navigationController popToRootViewControllerAnimated:YES];    //获取第一个控制器    UIViewController *oneVC = self.navigationController.viewControllers[0];    //返回指定控制器    [self.navigationController popToViewController:oneVC animated:YES];}@end

 

转载于:https://www.cnblogs.com/ios-g/p/4755645.html

你可能感兴趣的文章
WPF中实现多选ComboBox控件
查看>>
读构建之法第四章第十七章有感
查看>>
Windows Phone开发(4):框架和页 转:http://blog.csdn.net/tcjiaan/article/details/7263146
查看>>
Unity3D研究院之打开Activity与调用JAVA代码传递参数(十八)【转】
查看>>
python asyncio 异步实现mongodb数据转xls文件
查看>>
TestNG入门
查看>>
【ul开发攻略】HTML5/CSS3菜单代码 阴影+发光+圆角
查看>>
IOS-图片操作集合
查看>>
IO—》Properties类&序列化流与反序列化流
查看>>
测试计划
查看>>
Mysql与Oracle 的对比
查看>>
jquery实现限制textarea输入字数
查看>>
Codeforces 719B Anatoly and Cockroaches
查看>>
jenkins常用插件汇总
查看>>
c# 泛型+反射
查看>>
第九章 前后查找
查看>>
Python学习资料
查看>>
jQuery 自定义函数
查看>>
jquery datagrid 后台获取datatable处理成正确的json字符串
查看>>
ActiveMQ与spring整合
查看>>