屬性傳值 將A頁面所擁有的信息經過屬性傳遞到B頁面使用ide B頁面定義了一個naviTitle屬性,在A頁面中直接經過屬性賦值將A頁面中的值傳到B頁面。post
A頁面DetailViewController.h文件atom
#import <UIKit/UIKit.h>代理 #import "DetailViewController.h"server @interface RootViewController :UIViewController<ChangeDelegate>ci {rem UITextField *tf;get }編譯器 @endit
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]; } }
單例傳值(實現共享)
#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
#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; } @endA頁面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 //無返回值
Block傳值 例如A(Ablock)頁面的值傳道B(Bblock)頁面
在A頁面中ABlock.h @interface Ablock : UIViewController<UITableViewDelegate,UITableViewDataSource>
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
在A頁面中Bblock.h #import <UIKit/UIKit.h>
//// 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 |