1、 屬性傳值 將A頁面所擁有的信息經過屬性傳遞到B頁面使用html
很經常使用的傳值,也很方便,可是要拿到類的屬性。例如:web
B頁面定義了一個naviTitle屬性,在A頁面中直接經過屬性賦值將A頁面中的值傳到B頁面。ide
A頁面DetailViewController.h文件post
#import <UIKit/UIKit.h>atom
#import "DetailViewController.h"spa
@interface RootViewController :UIViewController<ChangeDelegate>3d
{代理
UITextField *tf;server
}htm
@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 ;
}
2、 代理傳值 delegate
拿不到屬性怎麼辦?使用代理吧~
A頁面push到B頁面,若是B頁面的信息想回傳(回調)到A頁面,用用代理傳值,其中B定義協議和聲明代理,A確認並實現代理,A做爲B的代理
A頁面RootViewController.h文件
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
@interface RootViewController : UIViewController<ChangeDelegate> //繼承代理,表示A類接受B類的協議
{
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
}
B頁面DetailViewController.h文件
#import <UIKit/UIKit.h>
//定義協議
@protocol ChangeDelegate <NSObject>
-(void)changeTitle:(NSString *)aStr;//聲明協議方法
@end
@interface DetailViewController : UIViewController
{
UITextField *textField;
//定義代理
id <ChangeDelegate> _delegate;
}
@property(nonatomic,assign)id<ChangeDelegate> delegate;
@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:UIBarButtonSystemItemDonetarget:selfaction:@selector(doneAction:)];
self.navigationItem.rightBarButtonItem = doneItem;
[doneItemrelease];
}
//pop回前一個頁面
-(void)doneAction:(id)sender
{
//若代理存在且響應了changeTitle這個方法
if (self.delegate && [self.delegaterespondsToSelector:@selector(changeTitle:)])
{
//[self.delegate changeTitle:textField.text];
//將textField.text參數傳給changeTitle方法 讓代理,也就是A類的對象去實現這個方法
[self.delegate changeTitle:textField.text];
NSLog(@"%@",self.navigationController.viewControllers);
[self.navigationControllerpopViewControllerAnimated: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.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;
//導航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];
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];
}
五、 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