1、自定義通知
第一步:定義一個方法
-(void)getCarName{
NSLog(@"getCarName");
}
第二步:注冊通知
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(getCarName)
name:@"event1"
object:nil];
參數
addObserver:添加該通知的類 selector:接收到通知後執行的方法 name:通知的名字,作為通知的標識 object:傳遞的對象第三步:發送廣播
我們通過創建一個按鈕的點擊事件來發送廣播
- (IBAction)notifyEvent:(id)sender {
[[NSNotificationCenter defaultCenter]postNotificationName:@"event1"
object:nil
userInfo:nil];
}
參數
postNotificationName:發送廣播的名字 object:傳遞的對象 userInfo:傳遞的參數第四步:移除通知
注冊了通知之後,必須記得移除通知,否則程序在退出時候回報錯
-(void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
2、監聽鍵盤通知
系統提供了監聽各種控件的通知,這裡演示鍵盤的通知
第一步:注冊通知
這裡注冊鍵盤將要彈出時和將要隱藏時的通知
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(show:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(hide:)
name:UIKeyboardWillHideNotification
object:nil];
第二步:實現對應方法
這裡會接受一個NSNotification,可以通過NSNotification獲取通知傳遞過來的信息
-(void)show:(NSNotification *) notification{
NSDictionary *userInfo = notification.userInfo;
NSLog(@"userInfo:%@",userInfo);
}
-(void)hide:(NSNotification *)notification{
}
當鍵盤彈出時,這裡會打印出鍵盤的各種信息,可以獲取鍵盤的Frame等信息
2017-03-14 01:02:24.857 DelegateDemo[1865:112324] userInfo:{
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
UIKeyboardIsLocalUserInfoKey = 1;
}
第三步:移除通知
別忘了移除通知哦
3、解決鍵盤遮蓋文本框問題
通過通知傳遞過來的參數可以計算出鍵盤應該偏移的y值,讓界面向上移動即可,效果圖如下

通過對通知的處理,完成對應的動畫即可,如果對y軸偏移量計算不懂的,可以在紙上畫一下就懂了
-(void)show:(NSNotification *) notification{
NSDictionary *userInfo = notification.userInfo;
NSLog(@"userInfo:%@",userInfo);
//獲取鍵盤的frame
CGRect keyBoardFrame = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
//獲取鍵盤的高度
CGFloat height = keyBoardFrame.size.height;
//獲取屏幕的高度
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
//計算出View的y抽偏移量
CGFloat dy = screenHeight - height - self.textField.frame.origin.y - self.textField.frame.size.height;
//如果被遮蓋
if(dy<0){
//執行動畫,將View的中心往上移動
[UIView animateWithDuration:1 animations:^{
self.view.center = CGPointMake(self.view.center.x, dy+screenHeight/2);
}];
}
}
-(void)hide:(NSNotification *)notification{
//執行動畫,恢復到屏幕原來的中心
[UIView animateWithDuration:1 animations:^{
self.view.center = CGPointMake(self.view.center.x, [UIScreen mainScreen].bounds.size.height/2);
}];
}
#pragma 代理方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//點擊非文本框讓文本框取消焦點,即隱藏鍵盤
[self.textField resignFirstResponder];
}
二、Delegate
1、自定義Delegate
第一步:在.h文件中,新建模型類,定義delegate的protocol文件
#import@interface CarModel : NSObject @property (nonatomic,strong)NSString *name; @property (nonatomic,strong)NSString *nickName; @end //自定義代理 @protocol CarModelDelegate //1、必須實現的回調方法 @required -(void)getCarModel:(CarModel *)model; //2、可選實現的回調方法 @optional -(void)getCarOtherModel:(CarModel *)model; @end
這裡提供兩個變量用於我們代理傳遞的參數,同時在代理中提供了兩個方法,有必選方法和可選方法
第二步:在.m文件中,聲明代理,觸發代理方法
我們直接將參數的初始化和代理方法的執行放在init方法中
#import "CarModel.h" @interface CarModel() //聲明代理 @property (nonatomic,weak)iddelegate; @end @implementation CarModel -(instancetype)init{ if(self = [super init]){ //初始化數據 self.name = @"奧迪"; self.nickName = @"A6"; //1、執行必選代理回調 if([self.delegate respondsToSelector:@selector(getCarModel:)]){ [self.delegate getCarModel:self]; } //2、執行可選代理回調 if([self.delegate respondsToSelector:@selector(getCarOtherModel:)]){ [self.delegate getCarOtherModel:self]; } } return self; } @end
第三步:在其他文件中,聲明代理,實現代理方法,調用代理方法
#import "ViewController.h" #import "CarModel.h" @interface ViewController ()@end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; CarModel *model = [[CarModel alloc]init]; [self getCarModel:model]; } -(void)getCarModel:(CarModel *)model{ NSLog(@"CarName-->%@",model.name); } @end
這裡在執行順序中,先執行CarModel的init方法,也就是將模型的代理方法已經初始化,也就是說實現方法中已經獲取模型傳遞過來的值了,所以執行代理方法getCarModel可以獲取model.name值。
以上就是IOS基礎通知代理之NSNotificationCenter、Delegate的全文介紹,希望對您學習和使用iOS開發有所幫助.[db:作者簡介][db:原文翻譯及解析]【iOS基礎通知代理之NSNotificationCenter、Delegate】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!