进击的KFC:UI(八)导航试图控制器,传值

来源:互联网 发布:java从入门到精通txt 编辑:程序博客网 时间:2024/05/29 08:30

一. UINavigationController导航视图控制器
导航视图控制器 :控制 其他视图控制器 的 控制器
其实它就是一个控制器(UIViewController),带一个view,带一个导航条)

AppDelegate.m中所做:让导航视图控制器作为window的根视图控制器 // 1.初始化一个视图控制器  RootViewController *rootVC = [RootViewController *]init];  // 2.创建一个导航视图控制器 ,把rootVC 给它     // 初始化时 需要一个根控制器 去显示   UINavigationController *nav =[ [UINavigationController *] initWithRootViewController:rootVC];   // 3.把导航视图控制器设置为self.window的根视图控制器// 现在 变成:把导航视图控制器 设置为self.window的根视图控制器 ,但是导航视图控制器 显示 的还是rootVC上的内容    // 注意:导航视图控制器UINavgationCOntroller 也是继承 UIViewControl的,这里利用了多态   self.window.rootViewController = nav;   // 4.释放    [nav release];    [ rootVC release];

UINavigationController 主要用于页面间的跳转
导航视图控制器,像栈一样管理那些视图控制器
4个方法:
// 视图控制器(UIVIewController)中 有一个导航控制器的属性
// 一旦视图控制器 被 导航视图控制器管理了,
// 那么 视图控制器中的 那个 导航控制器属性 就会被赋上值
// 并且,这个赋上的值就是 管理他的导航视图控制器
// push,pop方法是导航视图控制器的方法,所以要取出 视图控制器 的 导航视图控制器

 1.RootVC里 跳转到下一个页面:[self.navigationController pushViewController:SecondVC animated:YES];   2.SecondVC里回到上一个界面:[self.navigationController popViewControllerAnimated:YES];  // pop回去后,secondVC会被释放    3.ThirdVC里 直接返回到根视图:[self.navigationController popToRootViewControllerAnimated:YES];     4.ThirdVC里利用指定方法跳转到第二个界面:             // 1.先取出导航视图控制器 管理的数组                NSArray *array = self.navigationController.viewControllers;             // 2.取出第二界面 (不要创建新的,要返回的是原来的那个)                 SecondViewController *Second = array[1];              // 3.指定跳转                       [self.navigationController popToViewController:Second animated:YES];

二.UINavigationBar 导航栏
// 状态栏:高度20;
// 导航栏:横屏高度28; 竖屏高度44; 导航栏高就是44,状态栏的背景是导航栏延伸过去的,

// 设置导航栏的填充颜色self.navigationController.navigationBar.barTintColor = [UIColor greenColor];// 设置导航栏上的控件的填充颜色self.navigationController.navigationBar.tintColor = [UIColor orangeColor];// 设置导航栏的背景图片     // 1.图片高度 < 44,效果:平铺    // 2.图片高度 = 44,效果:只覆盖导航条,把状态栏露出来了    // 3.图片高度 > 44 && 图片高度 < 64 效果:平铺 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@“32064”]forBarMetrics:(UIBarMetricsDefault)]; // iOS7.0之后 ,导航条默认是半透明的,起始点是屏幕的左上角开始计算的// 关闭半透明:控件的起始点从(0,64)开始计算(就是从导航图下面开始计算)self.navigationController.navigationBar.translucent = NO;// 一般咱们使用默认的半透明状态 不用考虑起始点

三.UIBarButtonItem 导航栏按钮 (左,右变 按钮 中间title)
导航栏上的按钮 使用 UIBarButtonItem

// 1.通过字符串来添加按钮UIBarButtonItem *barButtonItemLeft = [[UIBarButtonItem alloc]initWithTitle:@"注册"style:(UIBarButtonItemStylePlain)target:self action:@selector(barButtonItemLeftClickL:)];// 设置为左按钮self.navigationItem.leftBarButtonItem = barButtonItemLeft;// 2.通过图片来添加按钮UIBarButtonItem *barButtonItemRight = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"right"]style:(UIBarButtonItemStylePlain)target:self action:@selector(barButtonItemRightClick:)];// 设置为右按钮self.navigationItem.rightBarButtonItem = barButtonItemRight;// 3.通过系统按钮来创建UIBarButtonItem *barButtonSys = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(barButtonSysClick:)];   // 设置右按钮,(添加导航栏按钮)self.navigationItem.leftBarButtonItem = barButtonSys;// 4.设置页面的导航栏 标题 (居中位置)self.navigationItem.title = @"KFC是做基的";// 还可以在标题位置设置一个视图UIVIew (x,y坐标不能影响位置,永远居中)   UIView  *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];   titleView.backgroundColor = [UIColor blueColor];   self.navigationItem.titleView = titleView;

四.传值
// 明确传值的位置
(1) 一般从前往后传 用:属性传值 的 步骤:1.先获取到要传的值 2.在第二个页面声明一个属性 这个属性用来接收传过来的值
(2)从后往前传 用:代理传值 : 谁传值,就在谁的.h文件中创建协议
具体讲解用代理传值:

   2.从后往前传值:利用代理 (难点)  (1)在SecondView里创建协议     @protocol SecondViewDelegate<NSObject>     // 声明一个方法用于传值     - (void)myChuanZhi:(NSString *)string;     @end   (2)在RootViewController.h里面遵循<SecondViewDelegate>协议   (3)在RootViewController.m里面实现协议方法      - (void)myChuanZhi:(NSString *)string      {           self.textField.text = string;       }    (4)在SecondVIewController.h里面上面代理属性           @property(assign,nonatomic)id<SecondViewDelegate>delegate;  //  // 所谓代理这个属性,就是一个遵循了协议的类的一个对象,说穿了,就是RootViewController的一个对象     (5)设置代理   // 就是给secondVC的delegate属性赋值    在RootViewController.m中的实现跳转方法中实现赋值    因为在跳转方法中,初始化了一个SecondVC,所以正好赋值到跳转过去的secondvc.delegate       SecondViewController *secondVC = [[SecondViewController alloc] init];       secondVC.delegate = self; // self就是RootVC这个遵循了代理的对象      (6)让代理去干活 (就是让代理去调用协议方法)      在SecondViewController.m中的点击跳转的方法里去实现           if([_delegate respondsToSelector:@selector(myChuanZhi:)])         {               [_delegate myChuanZhi:self.textField.text];         }
0 0
原创粉丝点击