您的位置:首页 > 产品设计 > UI/UE

UI-UINavigationController导航栏

2015-08-29 21:28 477 查看
//一般情况下,window的根视图控制器是容器控制器。
//系统中常用的两个容器控制器分别是UINavigationController和UITabBarController。
//创建一个navigationController,并且作为window的根视图控制器。
/**
UINavigationController
1.NavigationController是一个容器控制器,容器控制器的作用是用来管理一组视图控制器的。navigationController是以栈的方式来管理容器控制器的。
2.虽然navigationController是管理控制器的,但是控制器本身是不可见的,所以navigationController的View也管理者对应控制器的View。
3.navigationController的View整体上可以分为三部分:
1.navigationBar:导航条。导航条默认是透明色,在竖屏状态下的高度是44。
2.toolBar:工具条。工具条默认是隐藏的。
3.contentView:真正存放视图控制器View的View。contentView在导航条透明的情况下、在toolBar隐藏的状态下,是和整个屏幕等大的。如果导航条不透明,toolBar隐藏,那么contentView是从导航条的下面开始放置的。依次类推,如果navigationBar不透明、toolBar不隐藏,contentView真正的区域会很小。

4.导航控制器不能推出导航控制器。
5.导航控制器在管理视图控制器的时候,采用栈的数据结构进行管理,对应的容器为NSArray。视图控制器入栈采用的方法是push。导航控制器出栈采用的方法是pop。
6.navigationController自带一个导航条。在使用导航条的时候,每个被导航控制器管理的视图控制器都可以定制属于自己的导航条。当从一个视图控制器进入到另外一个控制器时,导航条上的内容会被清掉。

*/
UINavigationController *navgiationVC = [[UINavigationController alloc] initWithRootViewController:rootVC];

self.window.rootViewController = navgiationVC;
[navgiationVC release];
[rootVC release];
[self.window makeKeyAndVisible];


用UINavigationController实现3个视图间的跳转

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor orangeColor];
/***********设置导航条************/
self.navigationController.navigationBar.barTintColor = [UIColor yellowColor];
//设置标题
//self.navigationItem.title和self.title的区别在于:如果项目中既用到了navigationController又用到了tabBarController,用self.title会同时设置两个标题,用self.navigationItem.title只会设置navigation的标题。
self.navigationItem.title = @"首页";
//    self.title = @"首页";
//设置titleView为TextField。textField的placeholder为"请输入搜索内容"。
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 130, 30)];
textField.placeholder = @"请输入搜索内容";
textField.borderStyle = UITextBorderStyleRoundedRect;
self.navigationItem.titleView = textField;
[textField release];

//    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemCamera) target:self action:@selector(doTapAddButton:)];
//    self.navigationItem.rightBarButtonItem = barButtonItem;
//    [barButtonItem release];

//    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:<#(UIView *)#>]
//    self.navigationItem.rightBarButtonItem =

//    self.navigationItem.rightBarButtonItems = @[];

//创建一个label。label的坐标为(0,0,100,30).
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, 100, 30)];
label.text = @"哈哈哈";
[self.view addSubview:label];
[label release];

//添加一个按钮,当点击按钮时,进入第二个界面。
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(80, 150, 100, 30);
[button setTitle:@"进入第二个界面" forState:UIControlStateNormal];
[button addTarget:self action:@selector(doTapButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIButton *secondButton = [UIButton buttonWithType:UIButtonTypeSystem];
secondButton.frame = CGRectMake(80, 300, 100, 30);
[secondButton setTitle:@"进入第二个界面" forState:UIControlStateNormal];
[secondButton addTarget:self action:@selector(doTapSecondButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:secondButton];

}
#pragma mark - doTapButton
- (void)doTapButton:(UIButton *)button
{
//1.创建第二个界面。
SecondViewController *secondVC = [[SecondViewController alloc] init];
//2.推出第二个界面
//    [self presentViewController:secondVC animated:YES completion:nil];
[self.navigationController pushViewController:secondVC animated:YES];
//    [对应的navigationController  方法名];
//3.释放内存
[secondVC release];

}

4000
#pragma mark - 进入第二个界面
- (void)doTapSecondButton:(UIButton *)button
{
//1.创建第三个界面
ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
//2.推出第三个视图
[self.navigationController pushViewController:thirdVC animated:YES];
//3.释放内存
[thirdVC release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


第二个视图控制器

#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor greenColor];
//添加一个按钮,当点击按钮时,进入第二个界面。
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(80, 150, 200, 30);
[button setTitle:@"进入第三个界面" forState:UIControlStateNormal];
[button addTarget:self action:@selector(doTapButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
#pragma mark - doTapButton
- (void)doTapButton:(UIButton *)button
{
//1.创建第二个界面。
ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
//2.推出第二个界面
//    [self presentViewController:secondVC animated:YES completion:nil];
[self.navigationController pushViewController:thirdVC animated:YES];
//    [对应的navigationController  方法名];
//3.释放内存
[thirdVC release];

}


第三个视图控制器

#import "ThirdViewController.h"
#import "RootViewController.h"
@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor magentaColor];
//添加一个按钮,当点击按钮时,进入第二个界面。
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(80, 150, 200, 30);
[button setTitle:@"返回第一个界面" forState:UIControlStateNormal];
[button addTarget:self action:@selector(doTapButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
self.navigationController.hidesBarsOnTap = YES;

}
#pragma mark - doTapButton
- (void)doTapButton:(UIButton *)button
{

/**
*该方法是直接回到第一个界面。调用该方法时除了第一个界面其他界面都出栈。
*
*[self.navigationController popToRootViewControllerAnimated:<#(BOOL)#>]
*/
/**

*  这个方法是跳转到指定的控制器。一共需要两个参数
第一个参数是视图控制器(PS:必须是处在栈里面的控制器)
处于该视图控制器前面的控制器都会出栈。
第二个参数是否有动画效果。
self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>
*/

//该方法是将当前的视图控制器出栈。
//    self.navigationController popViewControllerAnimated:<#(BOOL)#>

[self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:YES];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息