1.經過屬性傳值web
將A頁面所擁有的信息經過屬性傳遞到B頁面使用,B頁面定義了一個naviTitle屬性,在A頁面中直接經過屬性賦值將A頁面中的值傳到B頁面。ide
#import <UIKit/UIKit.h>post
#import "DetailViewController.h"atom
@interface RootViewController :UIViewController<ChangeDelegate>spa
{3d
UITextField *textField;代理
}server
@endci
A頁面 RootViewController.m面實現文件rem
#import "RootViewController.h"
#import "DetailViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
//核心代碼
-(void)loadView
{
UIButton *BT = [UIButton buttonWithType:UIButtonTypeRoundedRect];
BT.frame = CGRectMake(0, 0, 100, 30);
[BT setTitle:@"Push" forState:0];
[BT addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)pushAction:(id)sender
{
textField = (UITextField *)[self.viewviewWithTag:1000];
//導航push到下一個頁面
//pushViewController 入棧引用計數+1,且控制權歸系統
DetailViewController *detailViewController = [[DetailViewControlleralloc]init];
//屬性傳值,直接屬性賦值
detailViewController.naviTitle =textField.text;
//導航push到下一個頁面
[self.navigationControllerpushViewController:detailViewController animated:YES];
[detailViewControllerrelease];
}
#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 ;
}
2.代理傳值
A頁面push到B頁面,若是B頁面的信息想回傳(回調)到A頁面,用代理傳值,其中B定義協議和聲明代理,A確認並實現代理,A做爲B的代理
A頁面RootViewController.h文件
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
@interface RootViewController : UIViewController<ChangeDelegate>
{
UITextField *textField;
}
@end
A頁面RootViewController.m實現文件
#import "RootViewController.h"
#import "DetailViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
//核心代碼
-(void)loadView
{
UIButton *BT = [UIButton buttonWithType:UIButtonTypeRoundedRect];
BT.frame = CGRectMake(0, 0, 100, 30);
[btn setTitle:@"Push" forState:0];
//A頁面push到B頁面
[BT addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:BT];
}
-(void)pushAction:(id)sender
{
textField = (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
{
textField = (UITextField *)[self.view viewWithTag:1000];
textField.text = aStr;//將從B頁面傳入的參數賦給A頁面中的TextField
textField.text = aStr;
}
B頁面DetailViewController.m文件
B 頁面 DetailViewController.h
#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.m實現文件
#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:UIBarButtonSystemItemDone target:self action:@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.delegate changeTitle:textField.text];//將textField.text參數傳給changeTitle方法 讓代理,也就是A頁面去實現這個方法
NSLog(@"%@",self.navigationController.viewControllers);
[self.navigationController popViewControllerAnimated:YES];
}
}
3.單例傳值(實現共享)
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
#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;
//導航push到下一個頁面
//pushViewController 入棧引用計數+1,且控制權歸系統
DetailViewController *detailViewController = [[DetailViewController alloc]init];
//導航push到下一個頁面
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
@end
B頁面DetailViewController.
#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];
}
4. 通知傳值 誰要監聽值的變化,誰就註冊通知 特別要注意,通知的接受者必須存在這一先決條件
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];
}
5.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