UI_08 UINavigationController、界面通信

喜夏-厌秋 提交于 2019-12-04 03:56:05

⼀、UINavigationController

     UINavigationController:导航控制器,是iOS中最常⽤的多视图控制器 之⼀,它⽤来管理多个视图控制器。 导航控制器可以认为是管理控制器的控制器,主要管理有层级关系的控制器。

     UINavigationController继承于UIViewController,以栈的⽅式管理所 控制的视图控制器,⾄少要有⼀个被管理的视图控制器,这个控制器 我们称作,导航控制器的根视图控制器。 任何继承⾃UIViewController的类(多态)都可以作为根控制器。

1、⼯作原理

  • UINavigationController通过栈的⽅式管理控制器的切换,控制⼊栈和出栈来展⽰各个视图控制器。

  • UINavigationController的ContentView⾥始终显⽰栈顶控制器的view。

  • viewControllers属性存储了栈中的所有被管理的控制器

  • navigationController属性,⽗类中的属性,每个在栈中的控制器,都能通过此属性,获取⾃⼰所在的UINavigationController对象。

2、⼊栈和出栈

  • - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; //进⼊下⼀个视图控制器

  • - (UIViewController *)popViewControllerAnimated:(BOOL)animated; //返回上⼀个视图控制器

  • - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; //返回到指定的视图控制器

  • - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated; //返回到根视图控制器

3、常⽤属性

  • viewControllers //所有处于栈中的控制器

  • topViewController //位于栈顶的控制器

  • visibleViewController //当前正在显⽰的控制器

  • navigationBar //导航条



⼆、定制UINavigationBar

     navigationBar—导航条,iOS7之后默认是透明的,iOS7之前默认是不 透明的。

     navigationBar在透明情况,与contentView会重合⼀部分区域。 navigationBar在不透明情况,contentView跟在navigationBar的下⾯。 navigationBar竖屏下默认⾼度44,横屏下默认⾼度32.


1、管理UINavigationItem

      UINavigationBar也是以栈的⽅式管 理⼀组UINavigationItem。提供push和pop操作item。

      每个视图控制器都有⼀个navigationItem属性。navigationItem中设 置的左按钮、右按钮、标题等,会随着控制器的显⽰,也显⽰到 navigationBar上

UINavigationBar

  • barStyle                      样式 

  • translucent                 透明度:YES时布局从状态栏顶端开始。NO时布局从状态栏底端开始

  • tintColor                     颜色

  • backgroundImage:     背景图片


//设置导航栏的barStyle(样式)translucent
   
//白色(默认)
   
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
   
self.navigationController.navigationBar.translucent = YES //(半透明)
   
self.navigationController.navigationBar.translucent = NO //(不透明)
   
//黑色
//    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
   
//设置导航栏的barTintColor
   
self.navigationController.navigationBar.barTintColor = [[UIColor brownColor] colorWithAlphaComponent:0.3];
   
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
   
////设置导航栏的backgroundImage,像素:6444,小于44
//    64
   
UIImage *image = [UIImage imageNamed:@"NavBar_64"];
    [
self.navigationController.navigationItem setPrompt:@"Prompt"];

    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

2、UINavigationItem

     UINavigationItem属于MVC中的M。封装了要显⽰在UINavigationBar上的数据。

  • title //标题

  • titleView //标题视图

  • leftBarButtonItem //左按钮

  • rightBarButtonItem //右按钮

//1、每一个导航控制器(NavigationController),有且只有一个导航栏(NavigationBar)
//2、导航栏上面展示的都是UINavigationItem里面的内容
//3、每一个视图控制器都有一个navigationItem
   
//设置导航栏上的UIBarButtonItem,三种设置方式
   
//a.系统样式
//    UIBarButtonItem *rightBI = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(clickRightBI:)];
   
//b.使用title初始化设置UIBarButtonItem
   
UIBarButtonItem *rightBI = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightBI:)];
   
//c.使用image初始化UIBarButtonItem,此时,image需要进行渲染设置,默认渲染为模版方式。需要将image渲染方式设置为原图方式
   
UIImage *image1 = [UIImage imageNamed:@"NavBtnLeft"];
   
UIImage *image2 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UIBarButtonItem *leftBI = [[UIBarButtonItem alloc] initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(clickRightBI:)];

    //添加Bar

    self.navigationItem.leftBarButtonItem = leftBI;
    [leftBI
release];
   
self.navigationItem.rightBarButtonItem = rightBI;
    [rightBI
release];
   
   
//设置导航栏上要显示的title
   
self.navigationItem.title = @"登录页面";
   
//设置导航栏上要显示的titleView

    self.navigationItem.titleView = [[UISegmentedControl alloc] initWithItems:@[@"消息", @"电话"]];

3、UIBarButtonItem

UIBarButtonItem属于MVC的M。定义了UINavigationItem上按钮的触

发事件,外观等

  • -initWithBarButtonSystemItem:target:action:

  • -initWithTitle:style:target:action:

  • -initWithImage:style:target:action:

  • tintColor

4、结构



三、界⾯间通信

1、属性传值

RegisterViewController.h

@property(nonatomic, retain) NSString *text;

LoginViewController.h

registerVC.text = @"someString";

2、代理传值

1.创建协议

RegisterViewControllerDelegate.h

#import <Foundation/Foundation.h>
@class NSString;

@protocol RegisterViewControllerDelegate <NSObject>

- (
void)sendValue:(NSString *)text;

@end

2.遵守、实现协议

LoginViewController.h

#import <UIKit/UIKit.h>

#import "RegisterViewControllerDelegate.h"

@interface LoginViewController : UIViewController<RegisterViewControllerDelegate>

@end

LoginViewController.m

- (void)sendValue:(NSString *)text
{
   
_label.text = text;

}

3.使用代理

RegisterViewController.h

//注意:声明一个代理属性,(因为registerVC是委托人,它要拥有一个代理属性,用来设置代理)

@property(nonatomic, assign)id<RegisterViewControllerDelegate> delegate;


RegisterViewController.m

- (void)clickBI:(UIBarButtonItem *)BI
{
    [
self.navigationController popViewControllerAnimated:YES];
    [
_delegate sendValue:@"hello"];

}

4.设置代理

LoginViewController.m
- (
void)clickBI:(UIBarButtonItem *)BI
{
   
RegisterViewController *registVC = [[RegisterViewController alloc] init];
   
    registVC.
delegate = self;
    [
self.navigationController pushViewController:registVC animated:YES];

}


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!