例如 :第二个界面中的lable显示第一个界面textField中的文本
这就需要用到属性传值、block传值
那么第一个视图控制器如何获的第二个视图控制器的部分信息
例如:第一个界面中的lable显示第二个界面textField中的文本
这就需要使用代理传值
页面间传值有八大传值方式,下面我们就简单介绍下页面间传值的几种方式:
(一)属性传值
第二个界面中的lable显示第一个界面textField中的文本
首先我们建立一个RootViewControllers和一个DetailViewControllers,在DetailViewControllers中声明一个textString属性,用于接收传过来的字符串,
同时创建一个Lable用来显示传过的字符串
在RootViewControllers上引入DetailViewControllers同时声明一个textField属性用来输入字符串
然后在RootViewControllers上我们创建并添加一个button,当点击button时响应相应方法进行视图间的切换完成视图间的传值
(二)Block传值
block传值也是从第二个界面给第一个界面传值
首先我们在DetailViewcontrollers的.h文件中,属性
在RootViewControllers的.m文件中,其他不变,在button的响应方法里我们为block属性赋值完成block传值
(三)代理传值
RootViewControllers页面push到DetailViewControllers页面,如果DetailViewControllers页面的信息想回传(回调)到RootViewControllers页面,用代理传值,其中DetailViewControllers定义协议和声明代理,RootViewControllers确认并实现代理,RootViewControllers作为DetailViewControllers的代理
首先在DetailViewControllers.h文件中我们创建协议方法
在DetailViewControllers的.m中我们判定代理对象存在时,为其绑定相应方法
RootViewControllers的.m文件中我们指定代理并让其执行代理的方法
(四)单例传值
单例传值(实现共享)
AppStatus.h 创建一个单例类 AppStatus
1#import <Foundation/Foundation.h>
2
3@interface AppStatus : NSObject
4 {
5 NSString*_contextStr;
6 }
7
8@property(nonatomic,retain)NSString*contextStr;
9
10 +(AppStatus *)shareInstance;
11
12 @end
AppStatus.m
1#import "AppStatus.h"
2
3@implementation AppStatus
4
5@synthesize contextStr = _contextStr;
6
7 static AppStatus *_instance = nil;
8
9 +(AppStatus *)shareInstance
10 {
11 if (_instance == nil)
12 {
13 _instance = [[super alloc]init];
14 }
15 return _instance;
16 }
17
18 -(id)init
19 {
20 if (self = [super init])
21 {
22
23 }
24 return self;
25 }
26
27 -(void)dealloc
28 {
29 [super dealloc];
30 }
31
32 @end
RootViewController.h
1 #import"RootViewController.h"
2 #import"DetailViewController.h"
3 #import"AppStatus.h"
4
5 @interface RootViewController ()
6
7 @end
8
9 @implementation RootViewController
10
11 -(void)loadView
12 {
13 //核心代码
14 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
15 btn.frame = CGRectMake(0,0, 100,30);
16 [btn setTitle:@"Push" forState:0];
17 [btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
18 [self.view addSubview:btn];
19 }
20
21 -(void)pushAction:(id)sender
22 {
23 tf = (UITextField *)[self.view viewWithTag:1000];
24
25 //单例传值 将要传递的信息存入单例中(共享中)
26 // [[AppStatus shareInstance]setContextStr:tf.text]; 跟下面这种写法是等价的
27 [AppStatus shareInstance].contextStr = tf.text;
28 //导航push到下一个页面
29 //pushViewController 入栈引用计数+1,且控制权归系统
30 DetailViewController *detailViewController = [[DetailViewController alloc]init];
31
32 //导航push到下一个页面
33 [self.navigationController pushViewController:detailViewController animated:YES];
34 [detailViewController release];
35 }
36
37 @end
DetailViewController.h
1 #import <UIKit/UIKit.h>
2 @protocol ChangeDelegate;//通知编译器有此代理
3
4 @interface DetailViewController: UIViewController
5 {
6 UITextField *textField;
7 }
8
9 @end
DetailViewController.m
1 #import"DetailViewController.h"
2 #import"AppStatus.h"
3
4 @interface DetailViewController ()
5
6 @end
7
8 @implementation DetailViewController
9
10 @synthesize naviTitle = _naviTitle;
11
12 -(void)loadView
13 {
14 self.view = [[[UIView alloc]initWithFrame:CGRectMake(0,0, 320,480)]autorelease];
15
16 //单例
17 self.title = [AppStatus shareInstance].contextStr;
18 textField = [[UITextField alloc]initWithFrame:CGRectMake(100,100, 150,30)];
19 textField.borderStyle = UITextBorderStyleLine;
20 [self.view addSubview:textField];
21 [textField release];
22
23 UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)];
24 self.navigationItem.rightBarButtonItem = doneItem;
25 [doneItem release];
26 }
27
28 //这个方法是执行多遍的 相当于刷新view
29 -(void)viewWillAppear:(BOOL)animated
30 {
31 [super viewWillAppear:animated];
32 tf = (UITextField *)[self.view viewWithTag:1000];
33 tf.text = [AppStatus shareInstance].contextStr;
34 }
35
36 //pop回前一个页面
37 -(void)doneAction:(id)sender
38 {
39 //单例传值
40 [AppStatus shareInstance].contextStr = textField.text;
41 [self.navigationController popToRootViewControllerAnimated:YES];
42 }
(五)通知传值
谁要监听值的变化,谁就注册通知 特别要注意,通知的接受者必须存在这一先决条件
A页面RootViewController.h
1 #import <UIKit/UIKit.h>
2 #import "DetailViewController.h"
3 @interface RootViewController : UIViewController<ChangeDelegate>
4 {
5 UITextField *tf;
6 }
7 @end
A页面RootViewController.m
1 #import"IndexViewController.h"
2 #import"DetailViewController.h"
3 #import"AppStatus.h"
4
5 @implementation IndexViewController
6
7 -(void)dealloc
8 {
9 [[NSNotificationCenter defaultCenter] removeObserver:self
10 name:@"CHANGE_TITLE" object:nil];
11 [super dealloc];
12 }
13
14 -(id)init
15 {
16 if (self = [super init])
17 {
18 [[NSNotificationCenter defaultCenter] addObserver:self
19 selector:@selector(change:)
20 name:@"CHANGE_TITLE"
21 object:nil];
22 }
23 return self;
24 }
25
26 -(void)change:(NSNotification *)aNoti
27 {
28 // 通知传值
29 NSDictionary *dic = [aNoti userInfo];
30 NSString *str = [dic valueForKey:@"Info"];
31
32 UITextField *tf = (UITextField *)[self.view viewWithTag:1000];
33 tf.text = str;
34 }
35
36 -(void)viewWillAppear:(BOOL)animated
37 {
38 [super viewWillAppear:animated];
39 /*
40 // 单例传值
41 UITextField *tf = (UITextField *)[self.view viewWithTag:1000];
42 tf.text = [AppStatus shareInstance].contextStr;
43 */
44 }
45
46 @end
DetailViewController.h
1 #import <UIKit/UIKit.h>
2 @protocol ChangeDelegate;//通知编译器有此代理
3
4 @interface DetailViewController: UIViewController
5 {
6 UITextField *textField;
7 }
8 @end
DetailViewController.m
1#import "DetailViewController.h"
2#import "AppStatus.h"
3
4 @implementation DetailViewController
5 @synthesize naviTitle = _naviTitle;
6
7 -(void)loadView
8 {
9 UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)];
10 self.navigationItem.rightBarButtonItem = doneItem;
11 [doneItem release];
12 }
13
14 // pop回前一个页面
15 -(void)doneAction:(id)sender
16 {
17 NSDictionary *dic = [NSDictionary dictionaryWithObject:textField.text forKey:@"Info"];
18
19 [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_TITLE" object:nil userInfo:dic];
20
21 [self.navigationController popViewControllerAnimated:YES];
22
23 }
// 另一个版本
属性传值 将A页面所拥有的信息通过属性传递到B页面使用 B页面定义了一个naviTitle属性,在A页面中直接通过属性赋值将A页面中的值传到B页面。
A页面DetailViewController.h文件
#import <UIKit/UIKit.h> #import "DetailViewController.h" @interface RootViewController :UIViewController<ChangeDelegate> { UITextField *tf; } @end
A RootViewController.m页面实现文件 #import "RootViewController.h" #import "DetailViewController.h"
@interface RootViewController () @end @implementation RootViewController //核心代码 -(void)loadView {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(0, 0, 100, 30); [btn setTitle:@"Push" forState:0]; [btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; }
-(void)pushAction:(id)sender { tf = (UITextField *)[self.viewviewWithTag:1000]; //导航push到下一个页面 //pushViewController 入栈引用计数+1,且控制权归系统
DetailViewController *detailViewController = [[DetailViewControlleralloc]init]; //属性传值,直接属性赋值 detailViewController.naviTitle =tf.text; //导航push到下一个页面 [self.navigationControllerpushViewController:detailViewController animated:YES]; [detailViewControllerrelease]; }
B页面DetailViewController.h文件
#import <UIKit/UIKit.h> @interface DetailViewController :UIViewController { UITextField *textField; NSString *_naviTitle; } @property(nonatomic,retain)NSString *naviTitle; @end
B页面.m实现文件
#import "DetailViewController.h" @interface DetailViewController () @end @implementation DetailViewController @synthesize naviTitle =_naviTitle; -(void)loadView { self.view = [[[UIViewalloc]initWithFrame:CGRectMake(0,0, 320,480)]autorelease]; self.title = self.naviTitle ; }
代理传值 A页面push到B页面,如果B页面的信息想回传(回调)到A页面,用用代理传值,其中B定义协议和声明代理,A确认并实现代理,A作为B的代理
A页面RootViewController.h文件
#import <UIKit/UIKit.h> #import "DetailViewController.h" @interface RootViewController : UIViewController<ChangeDelegate> { UITextField *tf; } @end
A页面RootViewController.m实现文件 #import "RootViewController.h" #import "DetailViewController.h"
@interface RootViewController () @end @implementation RootViewController //核心代码 -(void)loadView {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(0, 0, 100, 30); [btn setTitle:@"Push" forState:0]; //A页面push到B页面 [btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn];
}
-(void)pushAction:(id)sender { tf = (UITextField *)[self.view viewWithTag:1000]; //导航push到下一个页面 //pushViewController 入栈引用计数+1,且控制权归系统 DetailViewController *detailViewController = [[DetailViewController alloc]init];
//代理传值 detailViewController.delegate =self;//让其自身作为代理人 //导航push到下一个页面 [self.navigationController pushViewController:detailViewController animated:YES]; [detailViewController release]; }
//实现代理方法 -(void)changeTitle:(NSString *)aStr { tf = (UITextField *)[self.view viewWithTag:1000]; tf.text = aStr;//将从B页面传入的参数赋给A页面中的TextField tf.text = aStr; }
B页面DetailViewController.m文件
#import <UIKit/UIKit.h> @interface DetailViewController : UIViewController { UITextField *textField; //定义代理
id<ChangeDelegate>_delegate; }
@property(nonatomic,assign)id<ChangeDelegate> delegate; @end //定义协议 @protocol ChangeDelegate <NSObject> -(void)changeTitle:(NSString *)aStr;//协议方法 @end
B页面DetailViewController.h实现文件
#import "DetailViewController.h" @interface DetailViewController () @end @implementation DetailViewController -(void)loadView { self.view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];
UIBarButtonItem *doneItem = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:selfaction:@selector(doneAction:)]; self.navigationItem.rightBarButtonItem = doneItem; [doneItemrelease]; }
//pop回前一个页面 -(void)doneAction:(id)sender { if (self.delegate && [self.delegaterespondsToSelector:@selector(changeTitle:)])//若代理存在且响应了changeTitle这个方法 { //[self.delegate changeTitle:textField.text]; [self.delegatechangeTitle:textField.text];//将textField.text参数传给changeTitle方法 让代理,也就是A页面去实现这个方法 NSLog(@"%@",self.navigationController.viewControllers); [self.navigationControllerpopViewControllerAnimated:YES]; } }
单例传值(实现共享)
AppStatus.h 创建一个单例类 AppStatus #import <Foundation/Foundation.h>
@interface AppStatus : NSObject { NSString *_contextStr; } @property(nonatomic,retain)NSString *contextStr; +(AppStatus *)shareInstance;
@end
AppStatus.m
#import "AppStatus.h"
@implementation AppStatus @synthesize contextStr = _contextStr;
static AppStatus *_instance = nil; +(AppStatus *)shareInstance { if (_instance == nil) { _instance = [[super alloc]init]; } return _instance; }
-(id)init { if (self = [super init]) {
} return self; }
-(void)dealloc { [super dealloc]; }
@end
A页面RootViewController.h
#import "DetailViewController.h" #import "AppStatus.h"
@interface RootViewController ()
@end
@implementation RootViewController -(void)loadView { //核心代码 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(0, 0, 100, 30); [btn setTitle:@"Push" forState:0]; [btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; }
-(void)pushAction:(id)sender {
tf = (UITextField *)[self.view viewWithTag:1000];
//单例传值 将要传递的信息存入单例中(共享中) // [[AppStatus shareInstance]setContextStr:tf.text]; 跟下面这种写法是等价的 [AppStatus shareInstance].contextStr = tf.text; //导航push到下一个页面 //pushViewController 入栈引用计数+1,且控制权归系统
DetailViewController *detailViewController = [[DetailViewController alloc]init];
//导航push到下一个页面 [self.navigationController pushViewController:detailViewController animated:YES]; [detailViewController release]; } @end
B页面DetailViewController.h
#import <UIKit/UIKit.h> @protocol ChangeDelegate;//通知编译器有此代理
@interface DetailViewController : UIViewController { UITextField *textField; } @end
B页面DetailViewController.m #import "DetailViewController.h" #import "AppStatus.h"
@interface DetailViewController ()
@end
@implementation DetailViewController @synthesize naviTitle = _naviTitle;
-(void)loadView { self.view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];
//单例 self.title = [AppStatus shareInstance].contextStr;
textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)]; textField.borderStyle = UITextBorderStyleLine; [self.view addSubview:textField]; [textField release];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)]; self.navigationItem.rightBarButtonItem = doneItem; [doneItem release];
}
//这个方法是执行多遍的 相当于刷新view -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];
tf = (UITextField *)[self.view viewWithTag:1000]; tf.text = [AppStatus shareInstance].contextStr;
}
//pop回前一个页面 -(void)doneAction:(id)sender { // 单例传值 [AppStatus shareInstance].contextStr = textField.text; [self.navigationController popToRootViewControllerAnimated:YES]; }
通知传值 谁要监听值的变化,谁就注册通知 特别要注意,通知的接受者必须存在这一先决条件
A页面RootViewController.h #import <UIKit/UIKit.h> #import "DetailViewController.h"
@interface RootViewController : UIViewController<ChangeDelegate> { UITextField *tf; } @end
A页面RootViewController.m
#import "IndexViewController.h" #import "DetailViewController.h" #import "AppStatus.h"
@implementation IndexViewController
-(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:@"CHANGE_TITLE" object:nil]; [super dealloc]; }
-(id)init { if (self = [super init]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:@"CHANGE_TITLE" object:nil]; } return self; }
-(void)change:(NSNotification *)aNoti { // 通知传值 NSDictionary *dic = [aNoti userInfo]; NSString *str = [dic valueForKey:@"Info"];
UITextField *tf = (UITextField *)[self.view viewWithTag:1000]; tf.text = str; }
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];
/* // 单例传值 UITextField *tf = (UITextField *)[self.view viewWithTag:1000]; tf.text = [AppStatus shareInstance].contextStr; */ } @end
DetailViewController.h #import <UIKit/UIKit.h> @protocol ChangeDelegate;//通知编译器有此代理
@interface DetailViewController : UIViewController { UITextField *textField; } @end
DetailViewController.m
#import "DetailViewController.h" #import "AppStatus.h"
@implementation DetailViewController @synthesize naviTitle = _naviTitle;
-(void)loadView {
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)]; self.navigationItem.rightBarButtonItem = doneItem; [doneItem release]; }
// pop回前一个页面 -(void)doneAction:(id)sender {
NSDictionary *dic = [NSDictionary dictionaryWithObject:textField.text forKey:@"Info"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_TITLE" object:nil userInfo:dic];
[self.navigationController popViewControllerAnimated:YES];
}
Block 几种形式的Block //无返回值 void (^block1) (void); block1 = ^{ NSLog(@"bock demo"); }; block1();
//int返回类型 int (^block2) (void); block2 = ^(void) { int a = 1 ,b =1; int c = a+b; return c; };
//有返回 有参数 int (^block3)(int, int)= ^(int a, int b) { int c = a +b; return c;
}; NSLog(@"bock=%d",block3(1,2));
//有返回值,有参数并且可以修改block之外变量的block static int sum = 10;// __blcik and static关键字 或者 _block int sum = 10 int (^block4) (int) =^(int a) { sum=11; int c = sum+a; //此时sum就是可以修改的了,若没加static或_block关键字则不能修改block之外变量 return c; }; NSLog(@"block4= %d",block4(4));
Block传值 例如A(Ablock)页面的值传道B(Bblock)页面
在A页面中ABlock.h @interface Ablock : UIViewController<UITableViewDelegate,UITableViewDataSource> { UITableView *_tableview; UILabel *labe; UIImageView *imagevies; } @end
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [_tableview deselectRowAtIndexPath:indexPath animated:YES];
Bblcok *bblock = [[Bblcok alloc] initwithBlock:Block_copy(^(NSString *aBlock){ labe.text = aBlock; NSLog(@"%@",aBlock);
})];
bblock.imgeviews = imagevies.image; bblock.String = labe.text; [self.navigationController pushViewController:bblock animated:YES]; [bblock release]; }
在A页面中Bblock.h #import <UIKit/UIKit.h> typedef void (^MyBlock) (NSString *); @interface Bblcok : UIViewController { UIImageView *image; UITextField *aField; UIButton *aButt; NSString *_String; id _imgeviews; MyBlock myBlock; } @property(nonatomic,copy)MyBlock myBlock; @property(nonatomic,retain) id imgeviews; @property(nonatomic,retain) NSString *String; -(id)initwithBlock:(MyBlock)aBlcok; @end
// // Bblcok.m // Blcok // // Created by zhu on 13-8-12. // Copyright (c) 2013年 Zhu Ji Fan. All rights reserved. //
#import "Bblcok.h"
@interface Bblcok ()
@end
@implementation Bblcok @synthesize imgeviews = _imgeviews , String = _String; @synthesize myBlock = _myBlock; -(id)initwithBlock:(MyBlock)aBlcok { if (self = [super init]) { self.myBlock = aBlcok; } return self; }
-(void) dealloc { [super dealloc]; }
-(void) loadView { UIControl *cont = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 320, 568-44)]; [cont addTarget:self action:@selector(Clcik) forControlEvents:UIControlEventTouchUpInside]; self.view = cont;
aField = [[UITextField alloc] initWithFrame:CGRectMake(60, 10, 160, 30)]; aField.borderStyle = UITextBorderStyleLine; aField.placeholder = self.String; [self.view addSubview:aField];
aButt = [UIButton buttonWithType:UIButtonTypeRoundedRect]; aButt.frame = CGRectMake(60, 50, 70, 30); [aButt setTitle:@"修改" forState:0]; [aButt addTarget:self action:@selector(aButtClcik:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:aButt];
image = [[UIImageView alloc] initWithFrame:CGRectMake(60, 100, 210, 260)]; image.backgroundColor = [UIColor blueColor]; image.image = self.imgeviews; [self.view addSubview:image]; [image release];
}
-(IBAction)aButtClcik:(id)sender { NSString *sting = aField.text; myBlock(sting); [self.navigationController popToRootViewControllerAnimated:YES]; }
-(void)Clcik { [aField resignFirstResponder]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
@end |