界面间传值,分两种,一种情况是push入栈,从上一级视图到下一级视图,这只需要在下一级视图中,定义一个属性,用来把上一级的视图的值接收,在进入下一集视图之前封装好,然后在就可以在本视图中得到上一级传下来的值。
另一种是pop出栈,逆向传值,这样的情况,由于pop出栈的视图数据全部销毁,所以不能通过属性直接传值,此时可以用delegate来实现,声明一个协议,协议里定义一个可以传值的方法,参数即为要传出去的值。然后再在本视图中声明一个属性,遵循这个协议。这个视图就是相当于雇主,设置了一个协议,需要一个代理来实现这个协议的方法。此时就让上一级视图来接收这个协议,实现协议中的方法,这个时候,在下一级视图即将被pop出栈之前,用自己的代理来调用这个协议里的方法,此时的上一级的视图实现的方法的参数就可以赋给上一级视图的接受对象。
第一种(点击到下一级视图):
RootViewController.m
#import "RootViewController.h"
#import "FirstViewController.h"
@interface RootViewController ()
@property(nonatomic,retain)UITextField *tf;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 布局视图
[self p_setupView];
}
#pragma mark 布局视图
- (void)p_setupView{
self.view.backgroundColor = [UIColor yellowColor];
// textField
self.tf = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
self.tf.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_tf];
// rightBarButton
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemBookmarks) target:self action:@selector(rightBarButtonAction:)];
}
#pragma mark 右barButton点击方法
- (void)rightBarButtonAction:(UIBarButtonItem *)sender{
NSLog(@"右barButton被点击..");
// 创建firstVC
FirstViewController *firstVC = [[FirstViewController alloc]init];
// 使用属性接收值
firstVC.textStr = self.tf.text;
[self.navigationController pushViewController:firstVC animated:YES];
// 释放
[firstVC release];
firstVC = nil;
}
#pragma mark 响应触摸事件
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// 释放第一响应者,手键盘
[self.tf resignFirstResponder ];
// 阻断响应者链,也可以手键盘
// self.view.userInteractionEnabled = NO;
}
解释:
1、@property(nonatomic,retain)UITextField *tf;声明属性,用来传值。
2、FirstViewController *firstVC = [[FirstViewController alloc]init];创建下一级视图,是为了得到下一级视图接收传值对象。
3、firstVC.textStr = self.tf.text;在下一级中声明的属性testStr就是用来接收上一级传过去值的对象。到这里就可获取到上个视图传到下一个视图的值。
在push入栈之前完成(也就是传到下一级视图之前)。
FirstViewController.h
#import <UIKit/UIKit.h>
#import "PassValueDelegate.h"
@interface FirstViewController : UIViewController
// 用来接收上一个页面的值
@property(nonatomic,copy)NSString *textStr;
// 声明代理
@property(nonatomic,assign)id<PassValueDelegate>delegate;
@end
@property(nonatomic,copy)NSString *textStr;;即为声明的接收传值属性。
FirstViewController.m接收值
@font-face { font-family: "Courier New"; }@font-face { font-family: "宋体"; }@font-face { font-family: "Cambria Math"; }@font-face { font-family: "@宋体"; }@font-face { font-family: "Cambria"; }@font-face { font-family: "Heiti SC Light"; }@font-face { font-family: "@Heiti SC Light"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Cambria; }.MsoChpDefault { font-family: Cambria; }div.WordSection1 { page: WordSection1; }
#import "FirstViewController.h"
@interface FirstViewController ()
@property(nonatomic,retain)UITextField *tf;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self p_setupView];
// 赋值,得到上一级传过来的值
self.tf.text = self.textStr;
}
@end
self.tf.text = self.textStr;,获取到值
第二种情况:逆向传值
PassValueDelegate.h
声明一个协议
#import <Foundation/Foundation.h>
@protocol PassValueDelegate <NSObject>
- (void)passValue:(NSString *)aString;
@end
FirstViewController.h
#import <UIKit/UIKit.h>
#import "PassValueDelegate.h"
@interface FirstViewController : UIViewController
// 用来接收上一个页面的值
@property(nonatomic,copy)NSString *textStr;
// 声明代理
@property(nonatomic,assign)id<PassValueDelegate>delegate;
@end
@property(nonatomic,assign)id<PassValueDelegate>delegate;声明一个属性,遵循协议的属性。
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@property(nonatomic,retain)UITextField *tf;
@end
@implementation FirstViewController
#pragma mark 布局视图
- (void)p_setupView{
self.view.backgroundColor = [UIColor blueColor];
// textfield
self.tf = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
self.tf.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_tf];
// leftBarButton
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemReply) target:self action:@selector(leftButtonAction:)];
}
- (void)leftButtonAction:(UIBarButtonItem *)sender{
NSLog(@"点击左边..");
// 使用代理方法
[self.delegate passValue:self.tf.text];
[self.navigationController popViewControllerAnimated:YES];
}
在pop出栈之前,用代理调协议的方法参数就是要传过去的值。
RootViewController.h
#import <UIKit/UIKit.h>
#import "PassValueDelegate.h"
@interface RootViewController : UIViewController<PassValueDelegate>
@end
上一级视图(RootViewController),接收协议,遵循协议。
RootViewController.m
@font-face { font-family: "Courier New"; }@font-face { font-family: "宋体"; }@font-face { font-family: "Cambria Math"; }@font-face { font-family: "@宋体"; }@font-face { font-family: "Cambria"; }@font-face { font-family: "Heiti SC Light"; }@font-face { font-family: "@Heiti SC Light"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Cambria; }.MsoChpDefault { font-family: Cambria; }div.WordSection1 { page: WordSection1; }
#import "RootViewController.h"
#import "FirstViewController.h"
@interface RootViewController ()
@property(nonatomic,retain)UITextField *tf;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 布局视图
[self p_setupView];
}
// 实现协议方法
-(void)passValue:(NSString *)aString{
self.tf.text = aString;
}
#pragma mark 布局视图
- (void)p_setupView{
self.view.backgroundColor = [UIColor yellowColor];
// textField
self.tf = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
self.tf.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_tf];
// rightBarButton
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemBookmarks) target:self action:@selector(rightBarButtonAction:)];
}
#pragma mark 右barButton点击方法
- (void)rightBarButtonAction:(UIBarButtonItem *)sender{
// 创建firstVC
FirstViewController *firstVC = [[FirstViewController alloc]init];
// 设置代理
firstVC.delegate = self;
// 释放
[firstVC release];
firstVC = nil;
}
-(void)passValue:(NSString *)aString,实现协议方法,这个参数就是从下一级传进来的值,self.tf.text = aString;接收值。
但是,我们应该在创建下一级视图的同时,告诉雇主(下一级视图firstVC),你的代理是我。