29.导航控制器和页面间的传值

时间:2023-01-09 20:40:04

1.基本概念

  1. 导航控制器可以认为是管理控制器的控制器,主要管理有层级关系的控制器,任何继承⾃自UIViewController的类(多态)都可以作为根控制器,viewControllers属性存储了栈中的所有被管理的控制器。
  2. UINavigationController继承于UIViewController,以栈的方式管理所控制的视图控制器,至少要有一个被管理的视图控制器,这个控制器我们称作导航控制器的根视图控制器。
  3. UINavigationController通过栈的方式管理控制器的切换,控制入栈和出栈来展⽰示各个视图控制器。UINavigationController的ContentView里始终显示栈顶控制器的view。
  4. navigationBar—导航条,iOS7之后默认是半透明的,iOS7之前默认是不透明的。navigationBar在透明情况,与contentView会重合一部分区域。navigationBar在不透明情况,contentView紧跟在navigationBar的下面,所以我们通常通过属性translucent把它设置为不透明的。navigationBar竖屏下默认高度44,横屏下默认高度32.
  5. UINavigationBar除了能定义⾃身的样式外,还管理一组UINavigationItem。与UINavigationController相似,UINavigationBar也是以栈的方式管理一组UINavigationItem。提供push和pop操作item。
  6. barTintColor设置导航条的颜色 setBackgroundImage:forBarMetrics:导航条加背景图片.navigationController属性,父类中的属性,每个在栈中的控制器,都能通过此属性,获取自己所在的UINavigationController对象。
  7. UINavigationController以栈的方式管理视图控制器。通过push和pop控制跳转,每个视图控制器都有一个navigationItem属性。navigationItem中设置的左按钮、右按钮、标题等,会随着控制器的显示,也显示到navigationBar上.
  8. UINavigationBar管理一组UINavigationItem,UINavigationItem包含了UIBarButtonItem。
  9. 使⽤用属性传值解决从前往后传值的问题, 使⽤用delegate解决从后往前传值的问题

2.MainVIewController文件

#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@property(nonatomic,retain)UITextField *mainTxtField;
@end
#import "MainViewController.h"
#import "SecondViewController.h"

//4.签订协议
@interface MainViewController ()<SecondViewControllerDelegate>
@property(nonatomic,retain)UILabel *label;
@property(nonatomic,retain)UIButton *button;
@end
@implementation MainViewController

- (void)dealloc
{
[_button release];
[_label release];
[_mainTxtField release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];

self.view.backgroundColor = [UIColor greenColor];
self.navigationController.navigationBar.translucent = NO;

////第一种方式设置标题
self.title = @"猫眼电影";
//背景颜色的设置(不是所有的背景颜色都是backgroundColor)
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
//为了防止坐标系被篡改,我们把bar从半透明设置成不透明,这样坐标系的原点就贵自动向下推64
self.navigationController.navigationBar.translucent = NO;

//第二种方式设置标题
self.navigationItem.title = @"逗比青年";
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"信息",@"通话"]];
//指定一些视图,称为titleView
self.navigationItem.titleView = seg;
//创建左右两边的按钮(下面这个是系统提供的BarButtonItem),添加系统提供的或者自定义的BarButtonItem到导航栏上默认的显示颜色都为蓝色
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(letfButtonAcrion:)]autorelease];

//下面这个给方法rightBarButtonItem设置一个黑色的"充"的图片,添加上去系统会把它变为蓝色
//self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-chongzhi.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightclick:)]autorelease];
//下面这个给方法rightBarButtonItem设置一个黑色的"充"的图片,添加上去还是黑色
UIButton *buttton1 = [UIButton buttonWithType:UIButtonTypeCustom];
buttton1.frame = CGRectMake(0, 0, 40, 40);
[buttton1 setImage:[UIImage imageNamed:@"iconfont-chongzhi.png"] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttton1];

self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 140, 40)];
self.label.backgroundColor = [UIColor redColor];
[self.view addSubview:self.label];
[_label release];

self.mainTxtField = [[UITextField alloc] initWithFrame:CGRectMake(100, 180, 140, 40)];
self.mainTxtField.borderStyle = UITextBorderStyleRoundedRect;
self.mainTxtField.placeholder = @"请输入传值内容";
[self.view addSubview:self.mainTxtField];
[_mainTxtField release];

self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(100, 250, 140, 40);
[self.view addSubview:self.button];
self.button.layer.borderWidth = 0.4;
self.button.layer.cornerRadius = 10;
[self.button setTitle:@"下一页" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)letfButtonAcrion:(UIBarButtonItem *)button{
}

- (void)rightclick:(UIBarButtonItem *)button{
}

- (void)click:(UIButton *)button{
SecondViewController *secondVC = [[SecondViewController alloc] init];
//模态跳转页面
//[secondVC setModalPresentationStyle:UIModalPresentationPageSheet];
//[self presentViewController:secondVC animated:YES completion:^{
//}];
[self.navigationController pushViewController:secondVC animated:YES];
//通过属性向后面页面传值
secondVC.number = 100;
secondVC.str = self.mainTxtField.text;
secondVC.array = @[@"1",@"2"];
//后面页面值通过协议传回来
//5.设置代理人
secondVC.delegate = self;
[secondVC release];
}
//6.实现协议方法
- (void)changeValue:(NSString *)value{
self.label.text = value;
}

3.SecondViewController文件

#import <UIKit/UIKit.h>
//协议传值的第一步
//1.声明一份协议
@protocol SecondViewControllerDelegate <NSObject>
//协议方法
- (void)changeValue:(NSString *)value;
@end

@interface SecondViewController : UIViewController
//属性传值第一步,在第二个页面写一条属性
@property(nonatomic,assign)NSInteger number;
//针对字符串写一条属相
@property(nonatomic,copy)NSString *str;
@property(nonatomic,retain)NSArray *array;

//2.设置代理人的属性
@property(nonatomic,assign)id<SecondViewControllerDelegate> delegate;
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@property(nonatomic,retain)UITextField *textField;
@property(nonatomic,retain)UIButton *button;
@property(nonatomic,strong)UILabel *label;
@end
@implementation SecondViewController
- (void)dealloc
{
[_textField release];
[_button release];
[_label release];
[_array release];
[_str release];
[super dealloc];
}

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.title = @"第二页";

self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 140, 40)];
self.label.backgroundColor = [UIColor blueColor];
[self.view addSubview:self.label];
//把从MainViewController传过来的值显示在label里,即把属性里的值赋值给label
self.label.text = self.str;
[_label release];

self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 180, 140, 40)];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.placeholder = @"请输入返回内容";
[self.view addSubview:self.textField];
[_textField release];

self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(100, 250, 140, 40);
[self.view addSubview:self.button];
self.button.layer.borderWidth = 0.4;
self.button.layer.cornerRadius = 10;
[self.button setTitle:@"返回" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//打印从MainViewController传过来的值
NSLog(@"%ld",self.number);
NSLog(@"%@",self.array);
}

- (void)click:(UIButton *)button{
[self.navigationController popToRootViewControllerAnimated:YES];
//协议的触发条件是点击按钮
//3.设置代理人执行的协议方法
[self.delegate changeValue:self.textField.text];
}

4.简单的效果图如下

29.导航控制器和页面间的传值
29.导航控制器和页面间的传值
29.导航控制器和页面间的传值