3:通知傳值
通知中心
NSNotificationCenter提供了一種更加解耦的方式。最典型的應用就是任何對象對可以發(fā)送通知到中心,同時任何對象可以監(jiān)聽中心的通知。
發(fā)送通知的代碼如下:
[[NSNotificationCenter defaultCenter] postNotificationName:@”myNotificationName” object:broadcasterObject];
注冊接收通知的代碼如下:
[[NSNotificationCenter defaultCenter] addObserver:listenerObject selector:@selector(receivingMethodOnListener:) name:@”myNotificationName” object:nil];
注冊通知的時候可以指定一個具體的廣播者對象,但這不是必須的。你可能注意到了defaultCenter 。實際上這是你在應用中會使用到的唯一的中心。通知會向整個應用開放,因此只有一個中心。
同時還有一個NSDistributedNotificationCenter。這是用來應用間通信的。在整個計算機上只有一個該類型的中心。
優(yōu)點: 通知的發(fā)送者和接受者都不需要知道對方。可以指定接收通知的具體方法。通知名可以是任何字符串。
缺點: 較鍵值觀察需要多點代碼。在刪掉前必須移除監(jiān)聽者。 不能傳大量數(shù)值,只能讓誰去做什么事。
4:代理傳值(Delegate)
其中有兩個ViewController分別對應兩個界面,一個協(xié)議PassValueDelegate用來實現(xiàn)傳值協(xié)議,UserEntity是傳遞數(shù)據(jù)的對象。
以下是實現(xiàn)的效果:點擊Open進入Second界面,輸入完畢點擊OK后回到First界面并顯示結(jié)果
協(xié)議中聲明的方法:
copy
#import
@ class? UserEntity;
@protocol PassValueDelegate
-( void )passValue:(UserEntity *)value;
@end
在第一個窗口實現(xiàn)協(xié)議:
#import
#import "PassValueDelegate.h"
//第一個窗口遵守PassValueDelegate
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *nameLabel;
@property (retain, nonatomic) IBOutlet UILabel *ageLabel;
@property (retain, nonatomic) IBOutlet UILabel *gendarLabel;
- (IBAction)openBtnClicked:(id)sender;
@end
.m文件中實現(xiàn)協(xié)議的方法:
[cpp]view plaincopy
//實現(xiàn)協(xié)議,在第一個窗口顯示在第二個窗口輸入的值方法
-( void )passValue:(UserEntity *)value
{
self.nameLabel.text = value.userName;
self.ageLabel.text = [NSString stringWithFormat:@ "%d" ,value.age];
self.gendarLabel.text = value.gendar;
}
點擊Open按鈕所觸發(fā)的事件:
[cpp]view plaincopy
//點擊進入第二個窗口的方法
- (IBAction)openBtnClicked:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@ "SecondViewController"? bundle:[NSBundle mainBundle]];
//設置第二個窗口中的delegate為第一個窗口的self
secondView.delegate = self;
[self.navigationController pushViewController:secondView animated:YES];
}
第二個窗口中聲明一個NSObject對象,該對象遵守PassValueDelegate協(xié)議:
[cpp]view plaincopy
#import
#import "PassValueDelegate.h"
@interface SecondViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *nameTextField;
@property (retain, nonatomic) IBOutlet UITextField *ageTextFiled;
@property (retain, nonatomic) IBOutlet UITextField *gendarTextField;
//這里用assign而不用retain是為了防止引起循環(huán)引用。
@property(nonatomic,assign) NSObject *delegate;
- (IBAction)okBtnClicked:(id)sender;
- (IBAction)closeKeyboard:(id)sender;
@end
輸入完畢后,點擊OK按鈕所觸發(fā)的事件:
[cpp]view plaincopy
- (IBAction)okBtnClicked:(id)sender {
UserEntity *userEntity = [[UserEntity alloc] init];
userEntity.userName = self.nameTextField.text;
userEntity.gendar = self.gendarTextField.text;
userEntity.age = [self.ageTextFiled.text intValue];
//通過委托協(xié)議傳值
[self.delegate passValue:userEntity];
//退回到第一個窗口
[self.navigationController popViewControllerAnimated:YES];
[userEntity release];
}
以上就實現(xiàn)了使用Delegate在兩個ViewController之間傳值,這種場景一般應用在進入子界面輸入信息,完后要把輸入的信息回傳給前一個界面的情況,比如修改用戶個人信息,點擊修改進入修改界面,修改完后到顯示界面顯示修改后的結(jié)果。
5:Block傳值
1.第一頁中 聲明一個 block, 需要傳入一個顏色 , 讓當前的 view 變色
// 聲明一個 block, 需要傳入一個顏色 , 讓當前的 view 變色
void (^changeColor)( UIColor *color) = ^( UIColor *color){
self . view . backgroundColor = color;
};
2 .? 第一頁中 //block 傳值 --------- 將 block 給第二個頁面
SecondViewController *secondVC = [[ SecondViewController? alloc ]? init ];
//block 傳值 --------- 將 block 給第二個頁面
secondVC. block = changeColor;
3.第二頁中定義 -- 當 block 變量作為一個類的屬性 , 必須要使用 copy 修飾
//block 傳值 --------- 將 block 給第二個頁面
//block 傳值 --- 當 block 變量作為一個類的屬性 , 必須要使用 copy 修飾
@property ( nonatomic? ,? copy ) void (^block)( UIColor? *color);
4.在第二頁中給block傳值
//block 傳值 --------- 將傳值給 block
NSArray? *array = [ NSArray? arrayWithObjects :[ UIColor? yellowColor ], [ UIColor? cyanColor ], [ UIColor? greenColor ], [ UIColor? brownColor ],? nil ];
self . block ([array? objectAtIndex : rand () %? 4 ]);