UINavigationController 、界面通信

一、UINavigationController

二、定制UINavigationBar

三、界面间通信

一、UINavigationController

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

UINavigationController继承于UIViewController,以栈的方式管理所控制的视图控制器,

至少要有一个被管理的视图控制器,这个控制器我们称作,导航控制器的根视图控制器。

任何继承自UIViewController的类(多态)都可以作为根控制器。

工作原理

UINavigationController通过栈的方式管理控制器的切换,控制入栈和出栈来展示各个视图控制器。UINavigationController的ContentView里始终显示栈顶控制器view。viewControllers属性存储了栈中的所有被管理的控制器。

navigationController属性,父类中的属性,每个在栈中的控制器,都能通过此属性,获取自己所在的UINavigationController对象。

入栈出栈

pushViewController:animated//进入下一个视图控制器

popViewControllerAnimated://返回上一个视图控制器

popToViewController:animated //返回到指定的视图控制器

popToRootViewControllerAnimated//返回到根视图控制器

常用属性

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

topViewController //位于栈顶的控制器

visibleViewController//当前正在显示的控制器

navigationBar //导航条

二、定制UINavigationBar

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

navigationBar在透明情况,与contentView会重合一部分区域。

navigationBar在不透明情况,contentView跟在navigationBar的下面。

navigationBar竖屏下默认高度44,横屏下默认高度32.

自定义navigationBar

barTintColor //设置导航条的颜色

setBackgroundImage:forBarMetrics: //导航条加背景图片

管理UINavigationItem

UINavigationBar除了能定义自身的样式外,还管理一组UINavigationItem。与UINavigationController相似,UINavigationBar也是以栈的方式管理一组

UINavigationItem。提供push和pop操作item。

每个视图控制器都有一个navigationItem属性。

navigationItem中设置的左按钮、右按钮、标题等,会随着控制器的显示,也显示到navigationBar上。

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

title //标题

titleView //标题视图

leftBarButtonItem //左按钮

rightBarButtonItem //右按钮

UIBarButtonItem属于MVC的M。定义了UINavigationItem上按钮的触发事件,外观等

-initWithBarButtonSystemItem:target:action:

-initWithTitle:style:target:action:

-initWithImage:style:target:action:

tintColor

传值

1.属性传值:

一般用于上级页面传值到下级页面,属性声明在下级页面,用于接收上级传过来的值.

2.代理传值:

实质:通过协议方法中的<参数>实现值传递:如- (void)sendValue:(NSString *)text

口诀:①.哪个页面(类)需要值,这个页面(类)即代理,协议方法的实现和遵守协议以及设置代理就写在哪个页面(类)里.

②.声明代理属性,就写在传值的那个页面(类)里,这个页面(类)即委托人.

③.想什么时候将值传过去,那就是让[代理 执行协议方法:实参值];

//注意:如果想用好代理-协议:

①.要明确谁是委托人:registerVC

②.谁是代理:loginVC

③.委托人要让代理做什么事:RegisterViewControllerDelegate

其实:

registerVC只是负责将值传到上级页面(代理),代理接收到值,至于接收到的值,代理loginVC怎么处理,registerVC不管.

代码示例:

登陆页与注册页的切换:

LoginViewController.m
#import "LoginViewController.h"
#import "RegisterViewController.h"
@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor yellowColor];

    UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 400, 80, 40);
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"注册" forState:UIControlStateNormal];
    //添加点击事件
    [button addTarget:self action:@selector(enterRegister:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

#pragma mark-----UINavigationBar(导航栏)
  /*
     设置导航栏的barStyle(样式)和translucent(透明度)

   */
    //默认(白色,半透明)
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault ;
    self.navigationController.navigationBar.translucent = YES;
//第二种(白色,不透明)
//    self.navigationController.navigationBar.barStyle = UIBarStyleDefault ;
//    self.navigationController.navigationBar.translucent = NO;
//第三种(黑色,半透明)
//    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
//    self.navigationController.navigationBar.translucent = YES;
//第四种(黑色,不透明)
//    self.navigationController.navigationBar.barStyle =
//    UIBarStyleBlack;
//    self.navigationController.navigationBar .translucent = NO;
 /*

  设置导航栏的barTintColor(背景颜色),iOS 7.0新出的特性

  //如果要是想让透明起效果,不设置barTintColor,而是去设置backgroundColor
 */
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
/*
    设置导航栏上tintColor(导航栏上控件的颜色)
 */
    self.navigationController.navigationBar.tintColor =[UIColor redColor];
/*

 设置导航栏的backgroundImage,像素:64,44,小于44

 */
//64
    UIImage * image = [UIImage imageNamed:@"NavBar_64.png"];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
//44
//    UIImage * image = [UIImage imageNamed:@"NavBar_44.png"] ;
//    [self.navigationController.navigationBar
//     setBackgroundImage:image forBarMetrics:UIBarMetricsDefault ];
//<44
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_30.png"] forBarMetrics:UIBarMetricsDefault];

#pragma mark-----设置导航栏上要显示的内容:UINavigationItem

//①每一个导航控制器(NavigationController),有且只有一个导航栏(
//    NavigationBar)
//②每一个导航栏上面展示的都是UINavigationItem里面的内容
//③.每一个视图控制器都有一个navigationItem
/*
 设置导航栏上的UIBarButtonItem,三种设置方式
 */
////1.系统样式
//    UIBarButtonItem * rightBI = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(clickRightBI:)];
//    self.navigationItem.rightBarButtonItem = rightBI;
//
//    [rightBI release];
//2.使用title初始化设置UIBarButtonItem
    UIBarButtonItem * rightBI = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightBI:)];
    self.navigationItem.rightBarButtonItem = rightBI;
    [rightBI release];
//3.使用image初始化UIBarButtonItem,此时,image需要进行渲染设置,默认渲染为模板方式,我们需要将image渲染方式设置为原图方式
    //默认为模板方式
    UIImage * image1 = [UIImage imageNamed:@"NavBtnLeft.png"];
    //对图片进行渲染模式设置.注意:需要一个新的指针(对象)来接受

    UIImage * image2 =[image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

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

//设置导航栏上要显示的title
    self.navigationItem.title  [email protected]"登录页面";
//设置导航栏上要默认的titleView
//    self.navigationItem .titleView = [[UISegmentedControl alloc]initWithItems:@[@"消息",@"电话"]];

}
- (void)clickRightBI:(UIBarButtonItem *)BI
{
    NSLog(@"点击了rightBI");
}
- (void)enterRegister:(UIButton *)button
{//创建要推出的(入栈)的那个视图控制器
    RegisterViewController * registerVC =[[RegisterViewController alloc]init];
    //使用导航控制器推出下一级页面(将下一级页面对应的视图控制器解压入栈)
#warning----//属性传值

    registerVC.text =[button titleForState:UIControlStateNormal];

#warning mark----有关属性传值的错误
    //[register view];此法一般不用
    //值是传不过去,因为此时label并没有被创建

    //registerVC.label.text =[button titleForState:UIControlStateNormal];
    //使用导航控制器推出下一级页面(将下一级页面对应的视图控制器压入栈)
    [self.navigationController pushViewController: registerVC animated:YES ];
}

RegisterViewController.h

#import <UIKit/UIKit.h>

@interface RegisterViewController : UIViewController
//用来接受上个页面传过来的字符串
@property(nonatomic,retain)NSString * text;
@property(nonatomic,retain)UILabel * label;
@end

RegisterViewController.m

#import "RegisterViewController.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[ UIColor cyanColor];

    //将传过来的text显示在title上
    self.navigationItem.title = _text   ;

#warning mark----有关属性传值的错误

    self.label = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 150, 30)];

    [self.view addSubview:_label];
    _label.backgroundColor = [UIColor whiteColor];
    [_label release];

    UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 400, 80, 40);
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"登录" forState:UIControlStateNormal];
    //添加点击事件
    [button addTarget:self action:@selector(enterRegister:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}
- (void)enterRegister:(UIButton *)button
{   //1.返回上一级(此时此视图控制器出栈)
#warning ----此时出栈的那个视图控制器将被彻底销毁

    //.[self.navigationController popViewControllerAnimated:YES];
    //2.返回到第一级页面(显示栈底视图控制器,其中栈底和栈顶之间经历的所有视图控制器都会pop出栈,不需要手动pop );
    [self.navigationController popToRootViewControllerAnimated:YES];
    //3.返回到指定页面
    NSLog(@"%@",self.navigationController.viewControllers);
    //获取视图控制器管理的栈中所有的元素
    NSArray * array = self.navigationController.viewControllers;
    [self.navigationController popToViewController:array[0] animated:YES];
}

代理与属性传值

LoginViewController.h
#import <UIKit/UIKit.h>
#import "RegisterViewControllerDelegate.h"
//遵守协议
@interface LoginViewController : UIViewController<RegisterViewControllerDelegate>
@end
LoginViewController.m
#import "LoginViewController.h"
#import "RegisterViewController.h"
@interface LoginViewController ()
@property(nonatomic,retain)UILabel * label;
@property(nonatomic,retain)UITextField * textField;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor    = [UIColor yellowColor];

#pragma mark---创建UILabel,UITextField,UIBarButtonItem
    //用来显示下级传过来的字符串
    self.label =[[UILabel alloc]initWithFrame:CGRectMake(100, 300, 150, 30)];
    _label.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_label];
    [_label release];
    //2.将用户输入的值传到下一级页面
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 400, 150, 30)];
    _textField.borderStyle = UITextBorderStyleRoundedRect ;

    [self.view addSubview:_textField];

    [_textField release];
    //3.点击BarButtonItem推出下一级页面
    UIBarButtonItem * BI = [[UIBarButtonItem alloc]initWithTitle:@"PUSH" style:UIBarButtonItemStylePlain target:self action:@selector(clickBI:)];

    self.navigationItem.rightBarButtonItem = BI ;

    [BI release];

}
//当点击barButtonItem的时候,推出下一级,触发此方法
- (void)clickBI:(UIBarButtonItem *)BI
{
    RegisterViewController * registerVC = [[RegisterViewController alloc]init ];
    //此时属性传值
    registerVC.text = _textField.text;
    [self.navigationController pushViewController:registerVC animated:YES];
    //第四步,设置代理
    registerVC.delegate = self;
    [registerVC release];
}

#pragma mark----实现协议中的方法
//第三步:实现协议

- (void)sendValue :(NSString *)text

{
    _label.text = text;
}

RegisterViewControllerDelegate.h

#import <Foundation/Foundation.h>

@protocol RegisterViewControllerDelegate <NSObject>

//制定协议
- (void)sendValue:(NSString *)text;

@end

RegisterViewController.h

#import <UIKit/UIKit.h>
#import "RegisterViewControllerDelegate.h"
@interface RegisterViewController : UIViewController

//接受上级页面传过来的值
@property(nonatomic,retain)NSString * text;
//声明一个代理属性。(因为register是委托人,它要拥有一个delegate属性用来设置代理的)
@property(nonatomic,assign)id<RegisterViewControllerDelegate>delegate;
@end

RegisterViewController.m

#import "RegisterViewController.h"

@interface RegisterViewController ()
@property(nonatomic,retain)UILabel * label;
@property(nonatomic,retain)UITextField * textField;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor cyanColor];
    self.label =[[UILabel alloc]initWithFrame:CGRectMake(100, 300, 150, 30)];
    _label.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_label];
    [_label release];

#warning mark----将传过来的值,显示在label上
    _label.text = _text;
    //2.将用户输入的值传到下一级页面
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 400, 150, 30)];
    _textField.borderStyle = UITextBorderStyleRoundedRect ;

    [self.view addSubview:_textField];

    [_textField release];
    //3.点击BarButtonItem推出下一级页面
    UIBarButtonItem * BI = [[UIBarButtonItem alloc]initWithTitle:@"POP" style:UIBarButtonItemStylePlain target:self action:@selector(clickBI:)];

    self.navigationItem.leftBarButtonItem = BI ;

    [BI release];

}

- (void)clickBI:(UIBarButtonItem*)BI
{
    [self.navigationController popViewControllerAnimated:YES  ];

    //第六步:代理执行协议中的方法
    if ([_delegate respondsToSelector:@selector(sendValue:)])
      //检查该方法有没有实现,如果没有实现,就不执行这个方法,否者执行
    {

         [_delegate sendValue:_textField.text];
    }

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-27 10:24:09

UINavigationController 、界面通信的相关文章

iOS基础-UINavigationController、界面通信

UINavigationController 导航控制器 是iOS中最常用的多视图控制器之一,它用来管理多个视图控制器 导航控制器可以认为是管理控制器的控制器,主要管理有层级关系的控制器 导航控制器以栈的方式管理所控制的视图控制器,至少要有一个被管理的视图控制器,这个控制器我们称作导航控制器的根视图控制器 出栈和入栈pushViewController:animated:进入下一个视图控制器 popViewControllerAnimated:返回上一个视图控制器 popToviewContro

界面通信之属性传值、代理传值

界面通信有三种方法 1.属性传值 2.代理传值 3.blcok传值 先讲讲属性和传值和代理传值 //属性传值- (void)sendValue { SecondViewController *secondVC = [[SecondViewController alloc] init]; secondVC.string = _textField.text;//这句是属性传值的核心,在需要接收值的界面声明一个字符串属性接受值 secondVC.color = self.view.background

##DAY8 界面通信

##DAY8 界面通信 注意:延展中写的东西只能在类内使用 #pragma mark ———————属性传值—————————— (第一个页面往第二个页面传值) 一.属性传值:(第一个页面往第二个页面传值) 属性传值:可以自定义属性.也可使用控件.视图的自带属性 1.属性传值用于第一个界面向第二个页面传值 2.明确两者联系的桥梁,也就是触发跳转的地方 3.明确传输的值:类型是什么 4.在第二个视图控制器内部声明相对应类型的属性,来接收传输的值 5.在第二个界面使用传入的值 注意:属性在传值中如果

UI_08 UINavigationController、界面通信

?.UINavigationController UINavigationController:导航控制器,是iOS中最常?的多视图控制器 之?,它?来管理多个视图控制器. 导航控制器可以认为是管理控制器的控制器,主要管理有层级关系的控制器. UINavigationController继承于UIViewController,以栈的?式管理所 控制的视图控制器,?少要有?个被管理的视图控制器,这个控制器 我们称作,导航控制器的根视图控制器. 任何继承?UIViewController的类(多态)

UI基础:UINavigationController、界面通信

UINavigationControlle UINavigationController:导航控制器,是iOS中最常用的多视图控制器之一,它用来管理多个视图控制器.也称为多视图控制器. 导航控制器可以认为是管理控制器的控制器,主要管理有层级关系的控制器. 创建: UINavigationController继承于UIViewController,以栈的形式管理所 控制的视图控制器,至少要有1个被管理的视图控制器,这个控制器 我们称作,导航控制器的根视图控制器. 任何继承于UIViewContro

界面通信

属性传值.协议传值.Block传值 ?.属性传值 /**  *  属性传值     1.属性传值用于第一个界面向第二个界面传送值     2.明确二者联系的桥梁,也就是触发跳转的地方     3.明确传输的值 类型是什么     4.在第二个视图控制器内部声明相对应类型的属性,来接收传输的值     5.在第二个界面使用传入的值 */ //第?步:在SecondViewController.h?定义?个contents字符串属性来保存由第一个界面传过来的字符串内容@interface Secon

iOSDay27之界面通信

1. 属性传值(前面的界面给后面传值) 第一步: 在 SecondViewController.h 文件里定义一个存放传过来值的变量 contents 1 #import <UIKit/UIKit.h> 2 3 @interface SecondViewController : UIViewController 4 5 // 第一步:声明属性,用于存放传过来的值 6 @property (nonatomic, copy) NSString *contents; 7 8 @end 第二步: 在

iOS学习之界面通信

一.属性传值 在SecondViewController.h里 #import <UIKit/UIKit.h> @interface SecondViewController : UIViewController // 第一步:声明属性,用来存放上一页传过来的数据 @property (nonatomic, copy) NSString *contents; @end 在RootViewController.m里 - (void)loginAction { SecondViewControll

界面通信之block传值

block传值有两种方式 ?式?: 使?block属性实现回调传值 ?式?: 在?法中定义block实现回调传值 方式一比较便于理解而且常用,下面介绍方式一是如何传值的 使用block属性传值和代理传值的步骤基本上是一样的 typedef void(^BaDa)(NSString *, UIColor *); @interface SecondViewController : UIViewController @property (nonatomic, retain) UITextField *