IOS第12天(2,UINavigationController导航控制器)
2024-10-18 18:22:34
****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(, , , );
[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[];
//返回指定控制器
[self.navigationController popToViewController:oneVC animated:YES];
}
@end
最新文章
- 26、ASP.NET MVC入门到精通——后台管理区域及分离、Js压缩、css、jquery扩展
- 【代码笔记】iOS-给UIImageView加上圆角效果
- 使用虚幻引擎中的C++导论(二-UE4基类)
- lambda和委托
- 不使用synchronized和lock 锁实现线程安全单例
- SpringCloud Gateway 测试问题解决
- 使用ubuntu远程连接windows, Connect to a Windows PC from Ubuntu via Remote Desktop Connection
- mysql初步调试
- linux 安装开启SNMP协议,最下面是yum安装
- angular.js和ionic框架搭建一个webApp
- Python异常处理及元类
- (原创)Linux下MySQL 5.5/5.6的修改字符集编码为UTF8(彻底解决中文乱码问题)
- linux文件系统之loop环设备--新建一个文件系统并挂载
- YUV420视频上面添加字幕
- Cloneable 和clone的区别和联系
- synchronized同步方法和同步代码块的区别
- jdk及tomcat的安装
- golang sort包 排序
- Java 兔子问题(斐波那契数列)扩展篇
- 移动端使用rem时候动态设置html字体大小
热门文章
- DSP using MATLAB 示例Example3.4
- HTML元素的属性
- WPF ComboBox Binding
- Android PhoneGap 利用 Activity 实现 CordovaInterface
- jquery放大镜效果
- [WP8.1UI控件编程]Windows Phone XAML页面的编译
- [BZOJ2790][Poi2012]Distance
- 【CodeVS】 p1696 奇怪的函数
- Unity5.x版本AssetBundle加载研究
- iphone H5 input type=";search"; 不显示搜索 解决办法