1.UINavigationController的使用(多视图控制器)
1)UINavigationController的结构
2)UINavigationController创键
-UINavigationController继承于UIViewController,以栈的方式管理所控制的试图控制器,至少要有一个被管理的视图控制器
-创建的时候要提供一个是视图控制器作为导航控制器的一个根视图控制器
FirstViewController * firstVC = [[FirstViewControlleralloc]init];
UINavigationController * naviVC = [[UINavigationControlleralloc]initWithRootViewController:firstVC];
[_windowsetRootViewController: naviVC];
[naviVCrelease];
[firstVCrelease];
3)入栈和出栈
UINavigationController通过控制入栈和出栈来展示各个视图控制器
4)试图控制器常用切换方法
-进入下一个视图控制器
self.navigationController pushViewController: (UIViewController *) animated: (BOOL)
例:
SecondViewController * secondVC = [[SecondViewControlleralloc]init];
[self.navigationControllerpushViewController:secondVCanimated:YES];
[secondVCrelease];
-返回上一个视图控制器
self.navigationController popViewControllerAnimated:(BOOL)
例:
SecondViewController * secondVC = [[SecondViewControlleralloc]init];
[self.navigationController popViewControllerAnimated:YES]
[secondVCrelease];
-返回到指定的视图控制器
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
例:
[self.navigationControllerpopToViewController:[self.navigationController.viewControllersobjectAtIndex:1]animated:YES]
-返回到根视图控制器
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
例:
[self.navigationControllerpopToRootViewControllerAnimated:YES];
2.UINavigationBar
-对于navigationBar,ios7默认的高度是64.
-如果将navigationBar的透明度关闭之后,navigationBar的高度将会变为44.
自定义navigationBar
1)修改UINavigationBar的背景图片
//320 * 44 或者 320 * 64(navigationBar变成黑色) navigationBar的背景图片格式要求
[self.navigationController.navigationBarsetBackgroundImage:[UIImageimageNamed:@"1.jpg"]forBarMetrics:UIBarMetricsDefault];
2)修改UINavigationBar是否透明
//将navigationbar设置为不透明 这样屏幕左上角就不是(0,0)了,而是其下边缘
[self.navigationController.navigationBarsetTranslucent:NO];
透明状态
不透明状态
3)修改当前的Bar的背景颜色
[self.navigationController.navigationBar setBarTintColor:[UIColorlightGrayColor]];
-修改bar的颜色如果为黑其中的字都为白色,如果为默认那么字为黑色
[self.navigationController.navigationBarsetBarStyle:UIBarStyleBlack ];
默认的状态字为黑色
4)修改Bar上的标题title
//判断是否又Bar 有的话要显示在Bar上没有就不会显示
self.title = @"裴裴";
5)设置导航栏上的视图
1-//只能够取样式不会取功能只有在返回的时候才会出现一般不用 改变左侧键的样式
self.navigationItem.backBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"东东"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(buttonClicked:)];
2-设置左右按钮 可以设置方法
self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"傻瓜"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(buttonAction:)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"神经病" style:UIBarButtonItemStylePlain target:self action:@selector(buttonClicked:)];
3-有多个右侧按钮 放在一个数组里面
self.navigationItem.rightBarButtonItems = [NSArrayarrayWithObjects:[[UIBarButtonItemalloc]initWithTitle:@"傻瓜"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(buttonAction:)],[[UIBarButtonItemalloc]initWithTitle:@"神经病" style:UIBarButtonItemStylePlain target:self action:@selector(buttonClicked:)],nil];
self.navigationItem.leftBarButtonItems= [NSArrayarrayWithObjects:[[UIBarButtonItemalloc]initWithTitle:@"123"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(buttonAction:)],[[UIBarButtonItemalloc]initWithTitle:@"裴裴"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(buttonClicked:)],nil];
实现ViewController之间的互相转换
SecondViewController * secondVC = [[SecondViewControlleralloc]init];
[self.navigationControllerpushViewController:secondVCanimated:YES];
[secondVCrelease];
4-self.navigationItem是viewController的属性 因为并不是每个controller都有上面的按钮
navigationBar 是 navigationController的属性一直都不变的
5-修改中间的标题栏中间的视图
UILabel * label = [[UILabelalloc]initWithFrame:CGRectMake(0,0,100,100)];
label.backgroundColor = [UIColorblackColor];
//标题视图标题处其实是个view
self.navigationItem.titleView = label;
3.界面间传值
前面的界面一次传递给后面的界面信息
方法一:属性传值
第一个视图(根视图):FirstViewController
在该视图下设置的button方法里面
-(void)buttonAction:(UIButton *)button
{
SecondViewController * secondVC = [[SecondViewControlleralloc]init];
[self.navigationControllerpushViewController:secondVCanimated:YES];
将此时的button标题传入第二个视图的属性中去
secondVC.passValue = button.currentTitle;
}
第二个视图: SecondViewController
在想要获得值的那个视图中设置对应的属性 这里面以NSString为例
@property(nonatomic ,retain)NSString * passValue;
在- (void)viewDidLoad中要进行label的传值
label.text =self.passValue;(黄色为第二个视图的label)
方法二:单例传值
首先创建一个类继承与NSObject
在.h中声明自己的初始化方法和需要传值的对应属性
//1.单例是一个类的对象
//2.单例在整个程序中有且只有一个(对象)只能够赋值一次 可以没有
//3.单例一般使用加号+方法创建对象
+ (Singleton *)shareInstance;//单例的创建方法
//存储一个字符串数据
@property(nonatomic,retain)NSString * string;
3.在.m中实现方法
+ (Singleton *)shareInstance
{
//只有一次使用static创建一个空指针
//const是不能改变的
//这样一但赋值就没办法再次赋值了只能够调用;
staticSingleton * single =nil;
if (single ==nil) {
single = [[Singletonalloc]init];
}
return single;
}
还是在第一个视图和第二个视图之间传值
第一个视图的button方法:
-(void)buttonAction:(UIButton *)button
{
SecondViewController * secondVC = [[SecondViewControlleralloc]init];
[self.navigationControllerpushViewController:secondVCanimated:YES];
secondVC.passValue = button.currentTitle;
//利用 +号方法创建一个单例对象
Singleton * s = [SingletonshareInstance];
//利用单例给单例的属性传值
s.string = [buttoncurrentTitle];
[secondVCrelease];
}
第二个视图的ViewDidLoad
//利用单例获取想要的值
Singleton * s2 = [SingletonshareInstance];
label.text = s2.string;
应用于反向传值
方法一:代理传值(核心,关键)
1.指定协议
//给3视图控制器定义一个协议
@protocol thirdDelegate <NSObject>
//制定的协议方法
- (void)aa:(NSString *)value;
@end
2.设置代理属性
@property(nonatomic ,assign)id<thirdDelegate> delegate;
3.在需要发送数据的视图3的button中设置代理实现方法
- (void)buttonAction:(UIButton *)button
{
//在推出之前,让自己的代理执行方法
//目的给代理人(A)传第一个参数
[self.delegateaa: ((UITextField *)[self.viewviewWithTag:10000]).text];
[self.navigationControllerpopViewControllerAnimated:YES];
}
在视图2(即将接受数据的视图).h中导入视图3的.h文件,并且要签署视图3所指定的协议.
#import <UIKit/UIKit.h>
#import "ThirdViewController.h"
@interface SecondViewController :UIViewController<thirdDelegate>
视图2的button方法中将自己设置为视图3的代理:
-(void)buttonAction:(UIButton *)button
{
ThirdViewController * thirdVC = [[ThirdViewControlleralloc]init];
[self.navigationControllerpushViewController:thirdVCanimated:YES];
//设置第三个页面的代理人
thirdVC.delegate =self;
[thirdVCrelease];
}
在视图2中实现代理放法
- (void)aa:(NSString *)value
{
((UILabel *)[self.viewviewWithTag:10000]).text = value;
}