(一)属性传值:
属性传值是最简单,也是最常见的一种传值方式,但只能正向传值(将第一个页面的值传递到第二个页面,但无法从第二个页面传到第一个页面)
比如说有两个视图控制器:AViewController和BViewController,他们之间想从A传值到B,
(1)在AViewController中导入BViewController
#import "BViewController.h"
(2)定义全局变量
比如:我们在AViewController中加一个UITextfield控件,将他的值传递到B
@property (strong,nonatomic) UITextField *textA;
(3)在AViewController.m传值的方法中传值并到BVc
-(void)test
{
BViewController *BVc=[[BViewController alloc]init];
BVc.str=self.textA.text;//str为B中定义的NSString类型变量,用于接收A中textA中内容的
[self.navigationController pushViewController:BVc animated:YES];//跳转到BVc
}
在BVIewController.h文件中
@property(strong,nonatomic) NSString *str;//接收AVc中的内容
@property(strong,nonatomic) UITextField *BText;
在BViewController.m文件中
self.BText.text=self.str;//实现接收
这样我们就可以在B中随处使用传递过来的值;
(二)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
#import
@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
RootViewController.h
#import "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;
DetailViewController *detailViewController = [[DetailViewController alloc]init];31 32 [self.navigationController pushViewController:detailViewController animated:YES];34 [detailViewController release];35 } 36 37 @end
DetailViewController.h
@protocol ChangeDelegate;//通知编译器有此代理
@interface DetailViewController : UIViewController
{ UITextField *textField;}
DetailViewController.m
#import "DetailViewController.h"
#import "AppStatus.h"
@interface DetailViewController ()
@implementation DetailViewController
@synthesize naviTitle = _naviTitle;
-(void)loadView13
{ 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];}
-(void)viewWillAppear:(BOOL)animated
{ [super viewWillAppear:animated];
tf = (UITextField *)[self.view viewWithTag:1000];
tf.text = [AppStatus shareInstance].contextStr; }
-(void)doneAction:(id)sender
{ [AppStatus shareInstance].contextStr =textField.text;
[self.navigationController popToRootViewControllerAnimated:YES];
}
(五)通知传值
谁要监听值的变化,谁就注册通知 特别要注意,通知的接受者必须存在这一先决条件
A页面RootViewController.h
A页面RootViewController.m
#import "IndexViewController.h"
#import "DetailViewController.h"
#import "AppStatus.h"
@implementation IndexViewController
-(void)dealloc
{ [[NSNotificationCenter defaultCenter] removeObserver:selfname:@"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;
@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]; }
-(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];}