(一)屬性傳值:
屬性傳值是最簡單,也是最常見的一種傳值方式,但只能正向傳值(將第一個頁面的值傳遞到第二個頁面,但無法從第二個頁面傳到第一個頁面)
比如說有兩個視圖控制器: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傳值
(三)代理傳值
首先在DetailViewControllers.h文件中我們創建協議方法
在DetailViewControllers的.m中我們判定代理對象存在時,為其綁定相應方法
RootViewControllers的.m文件中我們指定代理并讓其執行代理的方法
(四)單例傳值
AppStatus.h ?創建一個單例類 AppStatus
?@interface AppStatus : NSObject
{? NSString *_contextStr; }
?@property(nonatomic,retain)NSString *contextStr;
+(AppStatus *)shareInstance;@end
AppStatus.m
?@synthesize contextStr = _contextStr;
? static AppStatus *_instance = nil;
? { ? _instance = [[super alloc]init];? }
-(void)dealloc{ ? [super dealloc];
RootViewController.h
?#import "RootViewController.h"
?#import "DetailViewController.h"
?@interface RootViewController () @end
@implementation RootViewController
{ 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]; }
{ ? 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"
?@interface DetailViewController ()
? @implementation DetailViewController
@synthesize naviTitle = _naviTitle;
{? ? 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];
self.navigationItem.rightBarButtonItem = doneItem;
-(void)viewWillAppear:(BOOL)animated
{ ? [super viewWillAppear:animated];
?tf = (UITextField *)[self.view viewWithTag:1000];
?tf.text = [AppStatus shareInstance].contextStr; }
{ [AppStatus shareInstance].contextStr =textField.text;
? [self.navigationController popToRootViewControllerAnimated:YES];
(五)通知傳值
誰要監聽值的變化,誰就注冊通知 ?特別要注意,通知的接受者必須存在這一先決條件
A頁面RootViewController.h
A頁面RootViewController.m
?#import "IndexViewController.h"
?#import "DetailViewController.h"
?@implementation IndexViewController
?{? ? [[NSNotificationCenter defaultCenter] removeObserver:selfname:@"CHANGE_TITLE" object:nil];
-(id)init? { if (self = [super init])
?{ ? ? ? [[NSNotificationCenter defaultCenter] addObserver:self? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? selector:@selector(change:)name:@"CHANGE_TITLE"? object:nil]; }
?-(void)change:(NSNotification *)aNoti
{ ??? NSDictionary *dic = [aNoti userInfo];
?NSString *str = [dic valueForKey:@"Info"];
?UITextField *tf =? (UITextField *)[self.view viewWithTag:1000];
?-(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"
@implementation DetailViewController
@synthesize naviTitle = _naviTitle;
{?? ? UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)];
? ? self.navigationItem.rightBarButtonItem = doneItem;
? -(void)doneAction:(id)sender
?{ NSDictionary *dic = [NSDictionary dictionaryWithObject:textField.text forKey:@"Info"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_TITLE" object:nil userInfo:dic];