UINavigationController : 继承于UIViewController
导航控制器, 是iOS中最常用的多视图控制器之一, 用来管理多个视图控制器
导航控制器以栈的方式管理所控制的视图控制器, 只要要有一个被管理的视图控制器, 这个控制器称作 : 导航控制器的根视图控制器
- 创建导航控制器的根视图控制器
首先需要创建一个继承于UIViewController的类:MainViewController, 然后在AppDelegate.m里创建
// 主要代码:
MainViewController *mainVC = [MainViewController alloc]init];
UINavigationController *naVC = [[UINacigationController alloc]initWithRootViewController: mainVC];
self.windwo.rootViewController = naVC;
[mainVC release];
[naVC release];
**不要忘记对dealloc 和 window进行释放**
- 在MainViewController.m中, 进行基本设置
- 注意 : 导航视图控制器的高度是44, 上面的状态栏高度是20, 加在一起默认高度是64
// 1.外观设置
背景颜色: (注意:不是所有的背景颜色都是backgroundColor)
self.navigationController.navigationBar.bartintColor = [UIcolor grayColor];
// 2.设置标题
self.title = @"猫眼电影" // 方法1
// 3.内容设置
self.navigationItem.title = @"猫眼电影"; // 设置标题:方法2
做一些指定的视图,作为titleView
UISegementedControl *seg = [[UISegementedControl alloc]initWithItem: @[@"信息", @"通话"]];
self.navigationItem.titleView = seg;
// 4.有关UINavigationBar
是导航条,默认为半透明, 为了防止坐标系被篡改,我们把它从透明设置成不透明,这样坐标系原点自动向下推64
self.nacigationController.navigationBar.translucent = NO;
// 5.创建左右两边按钮,默认左边是back
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(leftButtonAction:)] autorelease]; //系统自带按钮
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"32.png"] style: UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)]autorelease]; //自定义图案
//**创建按钮的时候,可以通过initWithBarButtonSystemItem来使用系统自带的按钮,也可以自定义图案,但用上面方式创建的图案为系统默认的蓝色,要想使自定义的图案以原来的形式显示, 可以进行如下改动:
UIButton *ringhtButton = [UIButton buttonWithType:UIButtonTypeCustom];
ringhtButton.frame = CGRectMake(0, 0, 40, 40);
[ringhtButton setImage:[UIImage imageNamed:@"32.png"] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:ringhtButton];
// 对应实现方法:
- (void)leftButtonAction:(UIBarButtonSystemItem)button{
NSLog(@"1");
}
- (void)rightButtonAction:(UIBarButtonSystemItem)button{
NSLog(@"2");
}
// 6.导航控制器控制页面跳转 (自带返回)
// ①创建一个跳转按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(200, 100, 100, 40);
[button setTitle:@"下一页" forState:UIControlStateNormal];
[self.view addSubview:button];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
// ②跳转方法
- (void)click:(UIButton *)button{
// 先创建下一页对象
SecondViewController *secVC = [[SecondViewController alloc]init];
// 跳转
[self.navigationController pushViewController:secVC animated:YES];
// 释放
[secVC release];
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
界面传值 : 前向后
// ①在后一个界面的.h文件里设置属性 (SecondViewController.h)
// 传数值
@property(nonatomic, assign)NSInteger number;
// 传字符串
@property(nonatomic, copy)NSString *str;
// 传数组
@property(nonatomic,copy)NSArray *arr;
// ②传值
//在第一个界面的跳转方法内写 (因为只有跳转的方法内会创建第二个页面的对象)
secVC.number = 100; // 传数值
secVC.str = self.myTextField.text; // 传字符 : 不要忘记建一个textfield
secVC.arr = @[@"杨林", @"刘珊珊"]; //传数组
界面传值 : 后向前 需要写协议
例:
点击button, 将textfield里输入的内容传到第一个界面的label里
–SecondViewController.m文件–
#import <UIKit/UIKit.h>
// 第一步 : 声明一份协议
@protocol SecondViewControllerDelegate <NSObject>
// 协议方法
- (void)changeValue:(NSString *)value;
@end
@interface SecondViewController : UIViewController
// 第二步 : 设置代理人属性
@property(nonatomic, assign)id<SecondViewControllerDelegate>delegate;
@end
–SecondViewController.m文件–
#import "SecondViewController.h"
@interface SecondViewController ()
// 一个textfiel, 一个button
@property(nonatomic, retain)UITextField *textfield;
@property(nonatomic, retain)UIButton *button;
@end
@implementation SecondViewController
- (void)dealloc
{
[_textfield release];
[_button release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
self.title = @"第二页";
self.textfield = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
self.textfield.layer.borderWidth = 1;
self.textfield.layer.cornerRadius = 10;
[self.view addSubview:self.textfield];
[_textfield release];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame = CGRectMake(100, 200, 150, 40);
[self.button setTitle:@"返回" forState:UIControlStateNormal];
self.button.backgroundColor = [UIColor redColor];
[self.view addSubview:self.button];
self.button.layer.borderWidth = 1;
self.button.layer.cornerRadius = 10;
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
// 点击返回
// 协议的触发条件是点击按钮, 所以在这里面进行协议传值的第三步
// 3.设置代理人执行的协议方法
- (void)click:(UIButton *)button{
[self.navigationController popToRootViewControllerAnimated:YES];
[self.delegate changeValue:self.textfield.text];
}
–MainViewController.m文件–
#import "MainViewController.h"
#import "SecondViewController.h"
// 第四步 : 签订协议
@interface MainViewController ()<SecondViewControllerDelegate>
@property(nonatomic, retain)UILabel *label;
@property(nonatomic, retain)UIButton *button;
@end
@implementation MainViewController
- (void)dealloc
{
[_label release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor orangeColor];
//设置不透明
self.navigationController.navigationBar.translucent = NO;
self.title = @"第一页";
self.label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
self.label.layer.borderWidth = 1;
[self.view addSubview:self.label];
[_label release];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame = CGRectMake(100, 200, 150, 40);
[self.button setTitle:@"下一页" forState:UIControlStateNormal];
self.button.backgroundColor = [UIColor redColor];
[self.view addSubview:self.button];
self.button.layer.borderWidth = 1;
self.button.layer.cornerRadius = 10;
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)click:(UIButton *)button{
// push下一页
SecondViewController *secVC = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
// 第五步 : 设置代理人
secVC.delegate = self;
}
// 第六步 : 实现协议方法
- (void)changeValue:(NSString *)value{
NSLog(@"%@", value);
self.label.text = value;
}