1.基本概念
- 导航控制器可以认为是管理控制器的控制器,主要管理有层级关系的控制器,任何继承⾃自UIViewController的类(多态)都可以作为根控制器,viewControllers属性存储了栈中的所有被管理的控制器。
- UINavigationController继承于UIViewController,以栈的方式管理所控制的视图控制器,至少要有一个被管理的视图控制器,这个控制器我们称作导航控制器的根视图控制器。
- UINavigationController通过栈的方式管理控制器的切换,控制入栈和出栈来展⽰示各个视图控制器。UINavigationController的ContentView里始终显示栈顶控制器的view。
- navigationBar—导航条,iOS7之后默认是半透明的,iOS7之前默认是不透明的。navigationBar在透明情况,与contentView会重合一部分区域。navigationBar在不透明情况,contentView紧跟在navigationBar的下面,所以我们通常通过属性translucent把它设置为不透明的。navigationBar竖屏下默认高度44,横屏下默认高度32.
- UINavigationBar除了能定义⾃身的样式外,还管理一组UINavigationItem。与UINavigationController相似,UINavigationBar也是以栈的方式管理一组UINavigationItem。提供push和pop操作item。
- barTintColor设置导航条的颜色 setBackgroundImage:forBarMetrics:导航条加背景图片.navigationController属性,父类中的属性,每个在栈中的控制器,都能通过此属性,获取自己所在的UINavigationController对象。
- UINavigationController以栈的方式管理视图控制器。通过push和pop控制跳转,每个视图控制器都有一个navigationItem属性。navigationItem中设置的左按钮、右按钮、标题等,会随着控制器的显示,也显示到navigationBar上.
- UINavigationBar管理一组UINavigationItem,UINavigationItem包含了UIBarButtonItem。
- 使⽤用属性传值解决从前往后传值的问题, 使⽤用delegate解决从后往前传值的问题
2.MainVIewController文件
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@property(nonatomic,retain)UITextField *mainTxtField;
@end
#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()<SecondViewControllerDelegate>
@property(nonatomic,retain)UILabel *label;
@property(nonatomic,retain)UIButton *button;
@end
@implementation MainViewController
- (void)dealloc
{
[_button release];
[_label release];
[_mainTxtField release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
self.navigationController.navigationBar.translucent = NO;
self.title = @"猫眼电影";
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.title = @"逗比青年";
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"信息",@"通话"]];
self.navigationItem.titleView = seg;
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(letfButtonAcrion:)]autorelease];
UIButton *buttton1 = [UIButton buttonWithType:UIButtonTypeCustom];
buttton1.frame = CGRectMake(0, 0, 40, 40);
[buttton1 setImage:[UIImage imageNamed:@"iconfont-chongzhi.png"] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttton1];
self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 140, 40)];
self.label.backgroundColor = [UIColor redColor];
[self.view addSubview:self.label];
[_label release];
self.mainTxtField = [[UITextField alloc] initWithFrame:CGRectMake(100, 180, 140, 40)];
self.mainTxtField.borderStyle = UITextBorderStyleRoundedRect;
self.mainTxtField.placeholder = @"请输入传值内容";
[self.view addSubview:self.mainTxtField];
[_mainTxtField release];
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(100, 250, 140, 40);
[self.view addSubview:self.button];
self.button.layer.borderWidth = 0.4;
self.button.layer.cornerRadius = 10;
[self.button setTitle:@"下一页" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)letfButtonAcrion:(UIBarButtonItem *)button{
}
- (void)rightclick:(UIBarButtonItem *)button{
}
- (void)click:(UIButton *)button{
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
secondVC.number = 100;
secondVC.str = self.mainTxtField.text;
secondVC.array = @[@"1",@"2"];
secondVC.delegate = self;
[secondVC release];
}
- (void)changeValue:(NSString *)value{
self.label.text = value;
}
3.SecondViewController文件
#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate <NSObject>
- (void)changeValue:(NSString *)value;
@end
@interface SecondViewController : UIViewController
@property(nonatomic,assign)NSInteger number;
@property(nonatomic,copy)NSString *str;
@property(nonatomic,retain)NSArray *array;
@property(nonatomic,assign)id<SecondViewControllerDelegate> delegate;
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@property(nonatomic,retain)UITextField *textField;
@property(nonatomic,retain)UIButton *button;
@property(nonatomic,strong)UILabel *label;
@end
@implementation SecondViewController
- (void)dealloc
{
[_textField release];
[_button release];
[_label release];
[_array release];
[_str release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.title = @"第二页";
self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 140, 40)];
self.label.backgroundColor = [UIColor blueColor];
[self.view addSubview:self.label];
self.label.text = self.str;
[_label release];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 180, 140, 40)];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.placeholder = @"请输入返回内容";
[self.view addSubview:self.textField];
[_textField release];
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(100, 250, 140, 40);
[self.view addSubview:self.button];
self.button.layer.borderWidth = 0.4;
self.button.layer.cornerRadius = 10;
[self.button setTitle:@"返回" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
NSLog(@"%ld",self.number);
NSLog(@"%@",self.array);
}
- (void)click:(UIButton *)button{
[self.navigationController popToRootViewControllerAnimated:YES];
[self.delegate changeValue:self.textField.text];
}
4.简单的效果图如下