掌握:
1. UINavigationController的使用:添加、移除控制器。
2. UINavigationBar内容的设置。
---------------------------------------------------------------------------------------------------------
一、控制器的添加和移除:
1. UINavigationController以栈的形式保存子控制器:
@property(nonatomic,copy) NSArray *viewControllers;
@property(nonatomic,readonly) NSArray *childViewControllers;
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
/** 下面方法中用作示意*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
// 1.创建导航控制器
XZOneViewController *one = [[XZOneViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:one]; // 传入一个栈底控制器来初始化导航控制器 (常用)
// 拿到栈顶控制器(显示在眼前的控制器)
// nav.topViewController
// 存放所有子控制器的栈
// nav.viewControllers
// 这个数组也存放子控制器
// nav.childViewControllers
// 2.添加子控制器
// XZOneViewController *one = [[XZOneViewController alloc] init];
// [nav addChildViewController:one]; // 这样子也能把one控制器放到 数组 viewControllers 和 childViewControllers 中。
// [nav pushViewController:one animated:YES]; // 将one压入栈中,即放入 viewControllers 和 childViewControllers 中。 (推荐用法,有动画)
// nav.viewControllers = @[one]; // 这样也是设置
// nav.viewControllers = @[one]; // 不能这么干,因为viewControllers是只读的。
// 3.设置为窗口的根控制器
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
@property(nonatomic,retain) UIBarButtonItem *backBarButtonItem;
@property(nonatomic,retain) UIView *titleView;
@property(nonatomic,copy) NSString *title;
@property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;
@property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;
二、控制器的view结构 以及 UINavigationBar导航条内容的设置:
1. 情景一:
self.navigationItem.title = @"第一个控制器";
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil];
self.navigationItem.rightBarButtonItems = @[item1, item2];
self.navigationItem.leftBarButtonItems = @[item1, item2];
2. 情景二:
self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];