一、使用UINavigationController的步骤以及代码
1 // 程序加载完成后执行的代码 2 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 3 // 1.初始化窗体 4 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 5 6 // 2.导航控制器的根控制器 7 UIViewController *vc = [[ViewController alloc] init]; 8 vc.view.backgroundColor = [UIColor lightGrayColor]; 9 // 3.窗体的根控制器为导航控制器 10 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; 11 12 // 4.设置窗体根控制器 13 self.window.rootViewController = nav; 14 15 // 5.显示窗体 16 [self.window makeKeyAndVisible]; 17 return YES; 18 }
@property(nonatomic,copy) NSArray *viewControllers;
@property(nonatomic,readonly) NSArray *childViewControllers;
<2>使用push方法能将某个控制器压入栈
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
<3>使用pop方法可以移除控制器
- (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;
<6>代码实现:
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 // 这段代码是错误的!--导航栏的内容由栈顶控制器的navigationItem属性决定,并不是有导航栏本身决定! 4 // self.navigationController.navigationItem.title = @"第一个导航"; 5 self.navigationItem.title = @"第一个导航"; 6 // 下面这一句跟上面的代码是同一个意思,简化写法 7 // self.title = @"这个管那里????"; 8 // 可以设置UIView 9 self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd]; 10 // 左右两边的按钮系统默认渲染成蓝色的,并且位置改动不了 11 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"没用" style:UIBarButtonItemStyleDone target:nil action:nil]; 12 // 右边按钮不受系统渲染的方法一:initWithCustomView 13 UIButton *btn = [[UIButton alloc] init]; 14 [btn setImage:[UIImage imageNamed:@"navigationbar_friendsearch"] forState:UIControlStateNormal]; 15 [btn setImage:[UIImage imageNamed:@"navigationbar_friendsearch_highlighted"] forState:UIControlStateHighlighted]; 16 [btn sizeToFit]; 17 18 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; 19 }