导航控制器的基本概念
◊ 导航控制器是用来管理子控制器的切换的大管家
◊ 导航控制器是个管理型的控制器, 它本身不显示视图内容, 而是管理子控制器的视图显示
◊ UINavigationController 是UIViewController的子类
导航控制器的结构
◊ 导航栏 ( Navigation bar ) ( 44/32 px)
◊ 子控制器的根视图
◊ 导航工具栏( Navigation toolbar ) , 默认隐藏,较少使用
栈结构管理子控制器
◊ 栈结构特点: 先进后出, 后进先出
导航栏的基本概念 ( UINavigationBar )
◊ 一个导航控制器包含一个导航栏 ( UINavigationBar )
◊ 一个子控制器对应一个导行项 ( UINavigationItem )
◊ 导航控制器管理一系列子控制器 ,UINavigationBar 管理一系列UINavigationItem , 每个UINavigationItem 和一个子控制器对应
设置导航栏
◊ 设置prompt属性
导航栏的prompt属性,通过navigationitem来设置, 其主要作用是用于提示用户. 比如, 用户正在请求网络数据时, 提示用户数据正在加载, 待加载完成后设为nil,取消显示
导航控制器的委托方法
◊ 设置代理方法
导航控制器的委托方法UINavigationControllerDelegate, 通过设置代理监听视图控制器的切换.
导航控制器小结
◊ 一个UINavigationController对应一个NavigationBar实例
◊ 一个UINavigationController可以包含多个子控制器UIViewController
◊ 每一个子控制器UIViewController对应一个UINavigationItem实例
◊ UINavigationItem控制多个UIBarButtonItem
常用方法
◊ 创建导航控制器
RootViewController *rootCtrl = [[RootViewController alloc] init];
// 创建导航控制器,并且将rootCtrl视图控制器作为导航控制器的跟控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootCtrl];
// 将导航控制器作为window的跟控制器
self.window.rootViewController = nab;
底层实现是将导航控制器的视图添加到window上显示,作为window的子视图
◊ 常用方法
// 将子视图控制器压入到导航控制器
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
// 弹出子视图控制器
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
// 弹出除根视图控制器外的所有子视图控制器
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
// 获取所有的子视图控制器
@property(nonatomic,copy) NSArray *viewControllers;
// 获取在栈中当前显示的视图控制器
@property(nonatomic,readonly,retain) UIViewController *visibleViewController;
// 获取在栈中最顶层的视图控制器
@property(nonatomic,readonly,retain) UIViewController *topViewController;
// 获取导航栏
@property(nonatomic,readonly) UINavigationBar *navigationBar;
◊ 三种pop弹出方法
// 弹到上一个控制器,可以选择是否需要动画效果
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
// 弹出到指定的视图控制器中
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
具体实现:
if(button.tag == 103) {
UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"提示"message:@"请输入索引" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:@"Go", nil];
/*
*设置alertView的样式:
UIAlertViewStylePlainTextInput UIAlertView弹出窗中带输入框的样式
*/
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
[alertView release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
//取得alertView上的UITextField的对象
UITextField *textField = [alertView textFieldAtIndex:0];
NSString *text = textField.text;
int index = [text intValue];
//取得所有的子控制器个数
NSArray *viewControllers = self.navigationController.viewControllers;
if (index >= 0 && index < viewControllers.count) {
UIViewController *viewController = [viewControllers objectAtIndex:index];
[self.navigationControllerpopToViewController:viewController animated:YES];
}
}
}
// 回到根视图控制器
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
◊ 使用导航栏内的标题
self.title = @"第一个视图控制器";
设置UIBarButtonItem 的按钮
// 第一种方式:
UIBarButtonItem *leftItem1 = [[UIBarButtonItemalloc] initWithTitle:@"menu" style:UIBarButtonItemStylePlain target:self action:@selector(menuAction:)];
//将按钮添加到navigationItem左侧
// self.navigationItem.leftBarButtonItem = leftItem1;
//2.第二种UIBarButtonItem的创建方式
UIBarButtonItem *leftItem2 = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:selfaction:@selector(cancelAction)];
// self.navigationItem.leftBarButtonItem = leftItem2;
//将多个按钮添加到navigationItem的左侧
self.navigationItem.leftBarButtonItems = @[leftItem1,leftItem2];
//3.第三种UIBarButtonItem的创建方式,自定义视图
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 68, 33);
[button setTitle:@"右侧" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:16];
[button setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightItem1 = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = rightItem1;
UINavigationItem设置标题, 标题视图
◊ 设置导航项上的标题
self.navigationItem.title = @"标题"; (会覆盖self.title的内容)
◊ 设置标题视图
UILabel *titleLabel = [[UILabelalloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
titleLabel.backgroundColor = [UIColorgrayColor];
titleLabel.font = [UIFontboldSystemFontOfSize:20];
titleLabel.textColor = [UIColorgreenColor];
titleLabel.text = @"标题";
titleLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = titleLabel;
[titleLabel release];
◊ 设置下一个控制器返回按钮的标题
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
UINavigationBar的设置
◊ 显示导航栏的显示样式
默认是蓝色
UIBarStyleBlackTranslucent 黑色半透明
UIBarStyleBlack 黑色
// 在导航栏的显示样式为UIBarStyleBlackTranslucent 可以设置背景颜色, 但颜色会被稍微处理 ( 只有在 UIBarStyleBlackTranslucent 样式中才可以 ,不建议使用该方法)
self.navigationController.navigationBar.backgroundColor = [UIColorredColor];
// 设置UINavigationBar 的颜色常用方法
self.navigationController.navigationBar.tintColor = [UIColorredColor];
/*
UIBarMetricsDefault 竖屏显示的图片
UIBarMetricsLandscapePhone 横屏显示的图片
*/
// [navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg_normal.png"]
// forBarMetrics:UIBarMetricsDefault];
//横屏显示的背景图片
// [navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg_normal.png"]
// forBarMetrics:UIBarMetricsLandscapePhone];
// self.navigationItem.prompt = @"正在保存…";
//设置是否显示底部的toolbar,默认是隐藏的
[self.navigationControllersetToolbarHidden:NOanimated:YES];
作业中的一些细节
◊ 将图片转成color颜色对象
UIColor *backColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"bj.jpg"]];
◊ 多行编辑输入框
UITextView *textView = [[UITextViewalloc] initWithFrame:CGRectMake(10, 10, 300, 100)];
textView.delegate = self;
textView.font = [UIFontsystemFontOfSize:16];
//弹出键盘
[textView becomeFirstResponder];
◊ 隐藏导航栏
[self.navigationController setNavigationBarHidden:YES animated:YES];
◊ 设置导航栏为半透明效果
self.navigationController.navigationBar.translucent = YES;