Objective--C 导航视图控制器
2015-12-24 19:48
513 查看
导航视图器相当于视图器的一个总管理者,在根视图创建后,任何一个视图控制器都能使用
一:创建
// 创建视图控制器
RootViewController *rootVC = [[RootViewController alloc] init];
// 创建导航视图控制器
UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
二:修改导航视图器
// 修改导航视图控制器的时候一般从外观和内容上进行修改
1 . 修改外观
// 修改导航视图控制器的半透明效果,默认是YES
self.navigationController.navigationBar.translucent = NO;
// 修改背景颜色
self.navigationController.navigationBar.barTintColor =
[UIColor yellowColor];
2.修改内容
// (1).标题
self.title = @"天猫首页";
// 改变标题
self.navigationItem.title = @"豆瓣电影";
// 可以把label放到titleView上,能改变label的内容,比如字体大小等
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
label.text = @"淘宝";
label.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = label;
label.font = [UIFont systemFontOfSize:25];
[label release];
// 不设置title(类似于QQ聊天界面的消息通话界面)
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"消息",@"通话"]];
self.navigationItem.titleView =
seg;
[seg release];
// (2)创建按钮
// 创建左边的按钮
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(barAction:)];
// 第二种方式(创建右边按钮,图片)
// self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"money.png"] style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)] autorelease];
// 第三种方式(创建右边确认按钮)
// self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
// 第四种,使用自定义视图(常用)
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"money.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 40, 40);
[button addTarget:self action:@selector(barAction:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithCustomView:button];
UIButton *pushBuootn = [UIButton buttonWithType:UIButtonTypeSystem];
pushBuootn.frame = CGRectMake(100, 100, 150, 50);
pushBuootn.layer.borderWidth = 1;
pushBuootn.layer.cornerRadius = 5;
[pushBuootn setTitle:@"下一页" forState:UIControlStateNormal];
[self.view addSubview:pushBuootn];
[pushBuootn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//跳转到下一页的Button点击事件
- (void)click:(UIButton *)button{
// 方式一:模态跳转
// 创建跳转到得页面
// SecondViewController *secVC = [[SecondViewController alloc] init];
// [secVC setModalTransitionStyle:0];
// [self presentViewController:secVC animated:YES completion:^{
//
// }];
// 方式二:通过导航视图控制器进行跳转操作
// 1.创建下一页的目标对象
SecondViewController *secVC = [[SecondViewController alloc] init];
// 2.跳转
[self.navigationController pushViewController:secVC animated:YES];
// 3.内存管理
[secVC release];
}
// 返回首页
[self.navigationController popToRootViewControllerAnimated:YES];
// 返回上一页
[self.navigationController popViewControllerAnimated:YES];
一:创建
// 创建视图控制器
RootViewController *rootVC = [[RootViewController alloc] init];
// 创建导航视图控制器
UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
二:修改导航视图器
// 修改导航视图控制器的时候一般从外观和内容上进行修改
1 . 修改外观
// 修改导航视图控制器的半透明效果,默认是YES
self.navigationController.navigationBar.translucent = NO;
// 修改背景颜色
self.navigationController.navigationBar.barTintColor =
[UIColor yellowColor];
2.修改内容
// (1).标题
self.title = @"天猫首页";
// 改变标题
self.navigationItem.title = @"豆瓣电影";
// 可以把label放到titleView上,能改变label的内容,比如字体大小等
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
label.text = @"淘宝";
label.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = label;
label.font = [UIFont systemFontOfSize:25];
[label release];
// 不设置title(类似于QQ聊天界面的消息通话界面)
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"消息",@"通话"]];
self.navigationItem.titleView =
seg;
[seg release];
// (2)创建按钮
// 创建左边的按钮
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(barAction:)];
// 第二种方式(创建右边按钮,图片)
// self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"money.png"] style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)] autorelease];
// 第三种方式(创建右边确认按钮)
// self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
// 第四种,使用自定义视图(常用)
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"money.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 40, 40);
[button addTarget:self action:@selector(barAction:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithCustomView:button];
UIButton *pushBuootn = [UIButton buttonWithType:UIButtonTypeSystem];
pushBuootn.frame = CGRectMake(100, 100, 150, 50);
pushBuootn.layer.borderWidth = 1;
pushBuootn.layer.cornerRadius = 5;
[pushBuootn setTitle:@"下一页" forState:UIControlStateNormal];
[self.view addSubview:pushBuootn];
[pushBuootn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//跳转到下一页的Button点击事件
- (void)click:(UIButton *)button{
// 方式一:模态跳转
// 创建跳转到得页面
// SecondViewController *secVC = [[SecondViewController alloc] init];
// [secVC setModalTransitionStyle:0];
// [self presentViewController:secVC animated:YES completion:^{
//
// }];
// 方式二:通过导航视图控制器进行跳转操作
// 1.创建下一页的目标对象
SecondViewController *secVC = [[SecondViewController alloc] init];
// 2.跳转
[self.navigationController pushViewController:secVC animated:YES];
// 3.内存管理
[secVC release];
}
// 返回首页
[self.navigationController popToRootViewControllerAnimated:YES];
// 返回上一页
[self.navigationController popViewControllerAnimated:YES];
相关文章推荐
- Objective-C基础1-类目(Category)和延展(Extension)的区别
- Objective-C 集合之NSArray的常用方法
- Objective-C NSObject 的实现分析(2014-10-23更新)
- Objective-C Runtime 总结:消息机制 篇
- Object.ReferenceEquals
- 遍历系统的所有ObjectType和TypeIndex
- iOS开发系列--Objective-C之KVC、KVO
- Objective-c语言_内存管理2(代码)
- Objective-C 和 Core Foundation 对象相互转换的内存管理总结
- Ubuntu snmpwalk 执行报错 “Unknown Object Identifier”
- ActiveX控件之ActiveXObject is not defined
- JSONObject、JSONArray
- Objective-C:Foundation框架-常用类-NSObject
- Objective-C:Foundation框架-常用类-NSDate
- Objective-C:Foundation框架-常用类-NSNull
- Objective-C:Foundation框架-常用类-NSValue
- Objective-C:Foundation框架-常用类-NSNumber
- Objective-C:Foundation框架-常用类-NSMutableDictionary
- ios objective-c java php des加解密统一
- Objective-C:Foundation框架-常用类-NSDictionary