基礎(chǔ) (二十) : 通知/代理

通知中心(NSNotificationCenter)

1.png

通知(NSNotification)

一個(gè)完整的通知一般包含3個(gè)屬性:(注意順序)

  • (NSString*)name; 通知的名稱

  • (id)object; 通知發(fā)布者(是誰(shuí)要發(fā)布通知)

  • (NSDictionary *)userInfo; 一些額外的信息(通知發(fā)布者傳遞給通知接收者的信息內(nèi)容)

初始化(可以理解為創(chuàng)建)一個(gè)通知(NSNotification)對(duì)象

只有通知的的名稱和通知的發(fā)布者

+(instancetype)notificationWithName:(NSString *)aName object:(id)anObject;

+(instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

  • (instancetype)initWithName:(NSString *)name object:(id)object
    userInfo:(NSDictionary *)userInfo;

發(fā)布通知

通知中心(NSNotificationCenter)提供了相應(yīng)的方法來(lái)幫助發(fā)布通知

-(void)postNotification:(NSNotification *)notification;

發(fā)布一個(gè)notification通知,可在notification對(duì)象中設(shè)置通知的名稱、通知發(fā)布者、額外信息等

-(void)postNotificationName:(NSString *)aName object:(id)anObject;

發(fā)布一個(gè)名稱為aName的通知,anObject為這個(gè)通知的發(fā)布者

-(void)postNotificationName:(NSString *)aName object:(id)anObject
userInfo:(NSDictionary *)aUserInfo;

發(fā)布一個(gè)名稱為aName的通知,anObject為這個(gè)通知的發(fā)布者,aUserInfo為額外信息

1.開(kāi)始監(jiān)聽(tīng)意思是(當(dāng)Bob監(jiān)聽(tīng)到了baby2發(fā)出的娛樂(lè)的通知,就會(huì)調(diào)用eat這個(gè)方法)

[[NSNotificationCenter defaultCenter] addObserver:Bob selector:@selector(eat:) name:@"娛樂(lè)" object:baby2];
 2.創(chuàng)建通知(baby2創(chuàng)建了一個(gè)叫娛樂(lè)的通知,并設(shè)置了通知的內(nèi)容)

NSNotification*note = [NSNotification notificationWithName:@"娛樂(lè)" object:baby2 userInfo:@{@"通知的key":@"通知的value"}];

 3.發(fā)布通知(三部曲)

[[NSNotificationCenter defaultCenter] postNotification:note];

 第二種簡(jiǎn)寫(xiě)方式


[[NSNotificationCenter defaultCenter] addObserver:baby selector:@selector(eat:) name:@"通知的名稱" object:baby2];

[[NSNotificationCenter

defaultCenter] postNotificationName:@"通知的名稱"
object:baby2 userInfo:@{@"通知的key":@"通知的value"}];

注冊(cè)通知監(jiān)聽(tīng)器

通知中心(NSNotificationCenter)提供了方法來(lái)注冊(cè)一個(gè)監(jiān)聽(tīng)通知的監(jiān)聽(tīng)器(Observer)

-(void)addObserver:(id)observer
selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;

observer:監(jiān)聽(tīng)器,即誰(shuí)要接收這個(gè)通知

aSelector:收到通知后,回調(diào)監(jiān)聽(tīng)器的這個(gè)方法,并且把通知對(duì)象當(dāng)做參數(shù)傳入

aName:通知的名稱。如果為nil,那么無(wú)論通知的名稱是什么,監(jiān)聽(tīng)器都能收到這個(gè)通知

anObject:通知發(fā)布者。如果為anObject和aName都為nil,監(jiān)聽(tīng)器都收到所有的通知

-(id)addObserverForName:(NSString *)name object:(id)obj
queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification
*note))block;

name:通知的名稱

obj:通知發(fā)布者

block:收到對(duì)應(yīng)的通知時(shí),會(huì)回調(diào)這個(gè)block

queue:決定了block在哪個(gè)操作隊(duì)列中執(zhí)行,如果傳nil,默認(rèn)在當(dāng)前操作隊(duì)列中同步執(zhí)行

取消注冊(cè)通知監(jiān)聽(tīng)器

通知中心不會(huì)保留(retain)監(jiān)聽(tīng)器對(duì)象,在通知中心注冊(cè)過(guò)的對(duì)象,必須在該對(duì)象釋放前取消注冊(cè)。否則,當(dāng)相應(yīng)的通知再次出現(xiàn)時(shí),通知中心仍然會(huì)向該監(jiān)聽(tīng)器發(fā)送消息。因?yàn)橄鄳?yīng)的監(jiān)聽(tīng)器對(duì)象已經(jīng)被釋放了,所以可能會(huì)導(dǎo)致應(yīng)用崩潰

通知中心提供了相應(yīng)的方法來(lái)取消注冊(cè)監(jiān)聽(tīng)器

-(void)removeObserver:(id)observer;

-(void)removeObserver:(id)observer name:(NSString
*)aName object:(id)anObject;

一般在監(jiān)聽(tīng)器銷(xiāo)毀之前取消注冊(cè)(如在監(jiān)聽(tīng)器中加入下列代碼):

-(void)dealloc {

[super dealloc]; 非ARC中需要調(diào)用此句

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

UIDevice通知

UIDevice類(lèi)提供了一個(gè)單粒對(duì)象,它代表著設(shè)備,通過(guò)它可以獲得一些設(shè)備相關(guān)的信息,比如電池電量值(batteryLevel)、電池狀態(tài)(batteryState)、設(shè)備的類(lèi)型(model,比如iPod、iPhone等)、設(shè)備的系統(tǒng)(systemVersion)

通過(guò)[UIDevice currentDevice]可以獲取這個(gè)單粒對(duì)象(一般用來(lái)做系統(tǒng)適配)

UIDevice對(duì)象會(huì)不間斷地發(fā)布一些通知,下列是UIDevice對(duì)象所發(fā)布通知的名稱常量:

UIDeviceOrientationDidChangeNotification 設(shè)備旋轉(zhuǎn)

UIDeviceBatteryStateDidChangeNotification 電池狀態(tài)改變

UIDeviceBatteryLevelDidChangeNotification 電池電量改變

UIDeviceProximityStateDidChangeNotification 近距離傳感器(比如設(shè)備貼近了使用者的臉部)

鍵盤(pán)通知

我們經(jīng)常需要在鍵盤(pán)彈出或者隱藏的時(shí)候做一些特定的操作,因此需要監(jiān)聽(tīng)鍵盤(pán)的狀態(tài)

鍵盤(pán)狀態(tài)改變的時(shí)候,系統(tǒng)會(huì)發(fā)出一些特定的通知

UIKeyboardWillShowNotification 鍵盤(pán)即將顯示

UIKeyboardDidShowNotification 鍵盤(pán)顯示完畢

UIKeyboardWillHideNotification 鍵盤(pán)即將隱藏

UIKeyboardDidHideNotification 鍵盤(pán)隱藏完畢

UIKeyboardWillChangeFrameNotification 鍵盤(pán)的位置尺寸即將發(fā)生改變

UIKeyboardDidChangeFrameNotification 鍵盤(pán)的位置尺寸改變完畢

系統(tǒng)發(fā)出鍵盤(pán)通知時(shí),會(huì)附帶一下跟鍵盤(pán)有關(guān)的額外信息(字典),字典常見(jiàn)的key如下:

UIKeyboardFrameBeginUserInfoKey 鍵盤(pán)剛開(kāi)始的frame

UIKeyboardFrameEndUserInfoKey 鍵盤(pán)最終的frame(動(dòng)畫(huà)執(zhí)行完畢后)

UIKeyboardAnimationDurationUserInfoKey 鍵盤(pán)動(dòng)畫(huà)的時(shí)間

UIKeyboardAnimationCurveUserInfoKey 鍵盤(pán)動(dòng)畫(huà)的執(zhí)行節(jié)奏(快慢)

  • (void)viewDidLoad {

    [super viewDidLoad];

    // 即將顯示的鍵盤(pán)

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
    name:UIKeyboardWillShowNotification object:nil];

    // 即將推出的鍵盤(pán)

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}

/**

  • 監(jiān)聽(tīng)鍵盤(pán)的即將顯示

*/

  • (void)keyboardWillShow:(NSNotification *)note
    {

// 獲得鍵盤(pán)的frame

CGRectframe = [note.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];

// 修改底部約束

self.bottn.constant= frame.size.height;

執(zhí)行動(dòng)畫(huà)

CGFloatduration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

[UIView animateWithDuration:duration animations:^{

    [self.view layoutIfNeeded];
}];

}

/**

  • 監(jiān)聽(tīng)鍵盤(pán)的即將隱藏

*/

  • (void)keyboardWillHide:(NSNotification *)note

{

// 修改底部約束

self.bottn.constant= 0;

// 執(zhí)行動(dòng)畫(huà)

CGFloatduration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

[UIView animateWithDuration:duration animations:^{

    [self.view layoutIfNeeded];

}];

}

  • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

// 文本框不再是第一響應(yīng)者,就會(huì)退出鍵盤(pán)

[self.textField resignFirstResponder];

[self.textField endEditing:YES];

[self.view endEditing:YES];

}

注意:監(jiān)聽(tīng)鍵盤(pán)的顯示和隱藏,最重要的思想就是拿整個(gè)view的高度減去鍵盤(pán)的高度,注意細(xì)節(jié)

通知和代理的選擇

共同點(diǎn)

利用通知和代理都能完成對(duì)象之間的通信

(比如A對(duì)象告訴D對(duì)象發(fā)生了什么事情, A對(duì)象傳遞數(shù)據(jù)給D對(duì)象)

不同點(diǎn)

代理 :一對(duì)一關(guān)系(1個(gè)對(duì)象只能告訴另1個(gè)對(duì)象發(fā)生了什么事情)

通知
:
多對(duì)多關(guān)系(1個(gè)對(duì)象能告訴N個(gè)對(duì)象發(fā)生了什么事情, 1個(gè)對(duì)象能得知N個(gè)對(duì)象發(fā)生了什么事情)

****************************************************************************************************************************************筆記**********************************************

框架搭建

框架搭建

  • 1.操作storyboard(toolBar+tableView)
  • 2.添加資料(plist,圖片,MJExtension)
  • 3.創(chuàng)建對(duì)應(yīng)模型/cell(xib注冊(cè))

  • 4.完善vc.m中的代碼(lazy

  • dataSouce + MJExtension)
  • (NSArray *)wines

{

if (!_wines) {

   替換key

[XMGWine setupReplacedKeyFromPropertyName:^NSDictionary *{

  return @{@"imaStr": @"image"};

}];

     plist->模型數(shù)組

_wines = [XMGWine objectArrayWithFilename:@"wines.plist"];

}

return _wines;

}

  • 5.cell的完善.(子控件,響應(yīng)事件)
  • 先禁止文本框的交互

pragma mark - cell自定義

  • (void)setWine:(XMGWine *)wine

{

_wine = wine;

self.icon_Ima.image = wine.icon;

self.name_Lab.text = wine.name;

self.money_Lab.text = wine.money;

self.buyCount_TF.text = [NSString stringWithFormat:@"%ld",
wine.buyCount];

}

  • (instancetype)cellWithTableView:(UITableView *)tableView

{

static NSString *identifier =nil;

if (identifier == nil)

{

identifier = [NSString stringWithFormat:@"%@ID", NSStringFromClass(self)];

[tableView registerNib:[UINib nibWithNibName:NSStringFromClass(self) bundle:nil]
forCellReuseIdentifier:identifier];

}

id cell = [tableView dequeueReusableCellWithIdentifier:identifier];

return cell;

}

customBtn

customBtn

IB_DESIGNABLE的宏的功能就是讓XCode動(dòng)態(tài)渲染出該類(lèi)圖形化界面。

IB_DESIGNABLE

@interface XMGCircleBtn ()

IBInspectable 讓支持KVC的屬性能夠在Attribute Inspector中配置。

@property (nonatomic, assign) IBInspectable CGFloat cornerRadius; /**< 圓角 */

@property (nonatomic, assign) IBInspectable CGFloat borderWidth; /**< 邊框?qū)?*/

@property (nonatomic, strong) IBInspectable UIColor borderColor; /*< 邊框顏色 */

@end

如果造成UI卡頓,記得寫(xiě)這兩行代碼

self.layer.shouldRasterize =YES;

self.layer.rasterizationScale =YES;

NSNotification

NSNotification

實(shí)例中的用法

先定義通知宏(const常量字符串)

  • 命名規(guī)范:所在類(lèi)+觸發(fā)發(fā)送的行為+Notification

define XMGWineCellPlusOnClickNotification @"XMGWineCellPlusOnClickNotification"

define XMGWineCellMinusOnClickNotification @"XMGWineCellMinusOnClickNotification"

define XMGWineCellMinusWine @"XMGWineCellMinusWine"

發(fā)送通知

  • 通知的發(fā)送一般都有觸發(fā)條件,比如按鈕的點(diǎn)擊

減號(hào)按鈕點(diǎn)擊發(fā)出的通知

[[NSNotificationCenter
defaultCenter] postNotificationName:XMGWineCellMinusOnClickNotification object:nil userInfo:@{XMGWineCellMinusWine:self.wine}];

加號(hào)按鈕點(diǎn)擊發(fā)出的通知

[[NSNotificationCenter
defaultCenter] postNotificationName:XMGWineCellPlusOnClickNotification object:self];

添加通知監(jiān)聽(tīng)

  • 一般是在控制器的viewDidLoad/viewDidAppear等方法中/或者自定義view的initWithFrame/Corder方法中添加
  • (void)viewDidLoad {

    [super viewDidLoad];

 讓控制器成為監(jiān)聽(tīng)者

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(cellPlusBtnOnClick:)
name:XMGWineCellPlusOnClickNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(cellMinusBtnOnClick:)
name:XMGWineCellMinusOnClickNotification object:nil];

}

監(jiān)聽(tīng)者接收通知后調(diào)用方法

  • 在收到通知后才會(huì)觸發(fā)調(diào)用

pragma
mark - NotificationSEL

  • (void)cellPlusBtnOnClick:(NSNotification *)note

{

[self.tableView reloadData];

XMGWine

*wine = [(XMGWineCell *)note.object wine];

 如果不包含這個(gè)模型先添加進(jìn)來(lái)

if (![self.winesInCar
containsObject:wine]) {

[self.winesInCar addObject:wine];

}



 計(jì)算應(yīng)該顯示的總價(jià)格

CGFloat

totalMoney = self.totalMoney_Label.text.doubleValue;

totalMoney += wine.money.doubleValue;

self.totalMoney_Label.text = [NSString
stringWithFormat:@"%.1f",
totalMoney];

}

  • (void)cellMinusBtnOnClick:(NSNotification *)note

{

[self.tableView reloadData];

XMGWine

*wine = note.userInfo[XMGWineCellMinusWine];

 如果購(gòu)物車(chē)模型中的購(gòu)買(mǎi)數(shù)量是0,那就移除

if (wine.count == 0 && [self.winesInCar containsObject:wine]) {

[self.winesInCar removeObject:wine];

}



 計(jì)算應(yīng)該顯示的總價(jià)格

CGFloat

totalMoney = self.totalMoney_Label.text.doubleValue;

totalMoney -= wine.money.doubleValue;

self.totalMoney_Label.text = [NSString
stringWithFormat:@"%.1f",
totalMoney];

}

移除監(jiān)聽(tīng)者

  • 有添加必須以后移除

有添加就需要移除

  • (void)dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

系統(tǒng)自帶通知的用法

  • 只監(jiān)聽(tīng)移除即可
  • 例如cell中監(jiān)聽(tīng)文本框文字變化
  • (void)awakeFromNib {

    Initialization code

[[NSNotificationCenter defaultCenter] addObserver:self
讓cell監(jiān)聽(tīng)通知

selector:@selector(textChange:)  收到通知后調(diào)用的方法

name:UITextFieldTextDidChangeNotification  通知的名稱

object:self.buyCount_TF];
發(fā)出通知的對(duì)象

}

文字改變的通知

  • (void)textChange:(NSNotification *)note {

    NSLog(@"%s, line = %d, note = %@",FUNCTION, LINE, note);

    此處只是演示作用,具體

    1.攔截判斷

 2.改變總價(jià)格



 根據(jù)當(dāng)前模型中的buyCount與當(dāng)前值對(duì)比,調(diào)用相應(yīng)次數(shù)的代理方法

}

  • (void)dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

Delegate

delegate

代理的使用步驟

  • 定義一份代理協(xié)議

  • 協(xié)議名字的格式一般是:類(lèi)名+ Delegate

  • 比如UITableViewDelegate

  • 代理方法細(xì)節(jié)

  • 一般都是@optional

  • 方法名一般都以類(lèi)名開(kāi)頭

  • 比如-
    (void)scrollViewDidScroll:

  • 一般都需要將對(duì)象本身傳出去

  • 比如tableView的方法都會(huì)把tableView本身傳出去

  • 必須要遵守NSObject協(xié)議

  • 比如@protocol
    XMGWineCellDelegate <NSObject>

  • 聲明一個(gè)代理屬性

  • 代理的類(lèi)型格式:id<協(xié)議>
    delegate

@property (nonatomic,
weak) id<XMGWineCellDelegate>
delegate;

  • 設(shè)置代理對(duì)象

xxx.delegate = yyy;

  • 代理對(duì)象遵守協(xié)議,實(shí)現(xiàn)協(xié)議里面相應(yīng)的方法

參考tableView的delegate三大步驟

  • 當(dāng)控件內(nèi)部發(fā)生了一些事情,就可以調(diào)用代理的代理方法通知代理

  • 如果代理方法是@optional,那么需要判斷方法是否有實(shí)現(xiàn)

if ([self.delegate
respondsToSelector:@selector(wineCell:didClicked:)])
{

[self.delegate wineCell:self

didClicked:XMGWineCellCalculatTypeMinus];

}

iOS監(jiān)聽(tīng)某些事件的方法

  • 通知(NSNotificationCenter\NSNotification)

  • 任何對(duì)象之間都可以傳遞消息

  • 使用范圍

  • 1個(gè)對(duì)象可以發(fā)通知給N個(gè)對(duì)象

  • 1個(gè)對(duì)象可以接受N個(gè)對(duì)象發(fā)出的通知

  • 必須得保證通知的名字在發(fā)出和監(jiān)聽(tīng)時(shí)是一致的

  • KVO

  • 僅僅是能監(jiān)聽(tīng)對(duì)象屬性的改變(靈活度不如通知和代理)

  • 代理

  • 使用范圍

  • 1個(gè)對(duì)象只能設(shè)置一個(gè)代理(假設(shè)這個(gè)對(duì)象只有1個(gè)代理屬性)

  • 1個(gè)對(duì)象能成為多個(gè)對(duì)象的代理

  • 通知規(guī)范

  • 建議使用優(yōu)先級(jí)代理->通知

UIAlertController

UIAlertController

  • 來(lái)源于UIAlertView/UIActionSheet
  • (void)testAlert {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert"

          message:@"UIAlertView is deprecated. Use UIAlertController

with a preferredStyle of UIAlertControllerStyleAlert instea"

delegate:self

cancelButtonTitle:@"取消"

otherButtonTitles:@"確定", nil];

[alert

show];

}

pragma
mark - UIAlertViewDelegate

  • (void)alertView:(UIAlertView *)alertView
    clickedButtonAtIndex:(NSInteger)buttonIndex

{

 根據(jù)buttonIndex判斷是哪個(gè)按鈕點(diǎn)擊了

}

  • 不必再設(shè)置代理
  • (void)testAlertController {
 是用來(lái)替代UIAlertView/UIActionSheet

 1.創(chuàng)建提示框

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"alertController" 標(biāo)題

message:@"描述~"

preferredStyle:UIAlertControllerStyleAlert]; alert/actionSheet

 2.添加按鈕和響應(yīng)事件

 2.1 創(chuàng)建

UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"I know" 按鈕文字

style:UIAlertActionStyleCancel 按鈕樣式

handler:^(UIAlertAction *action) {  按鈕響應(yīng)事件

NSLog(@"%s, line = %d",__FUNCTION__, __LINE__);

}];

 2.2 添加到提示框上

[alert

addAction:action0];

 3.展示提示框

[self presentViewController:alert animated:YES completion:nil];

}

  • 注意文本框只能添加到Alert格式上

keyBoardEditing(鍵盤(pán)處理):

keyBoardEditing

pragma mark - 鍵盤(pán)處理

  • (void)viewDidLoad {

    [super viewDidLoad];

self.tableView.rowHeight = 80;

 監(jiān)聽(tīng)鍵盤(pán)的frame變化

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardTransform:)
name:UIKeyboardWillChangeFrameNotification object:nil];

}

通知響應(yīng)的方法

  • (void)keyboardTransform:(NSNotification *)note

{

NSLog(@"%s, line = %d, %@",__FUNCTION__, __LINE__, note.userInfo);

/*

 打印的note.userInfo

UIKeyboardAnimationCurveUserInfoKey = 7;

UIKeyboardAnimationDurationUserInfoKey = "0.25";

UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 216}}";

UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 775}";

UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 559}";

UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375,
216}}";

UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 451}, {375,
216}}";

*/

 拿到鍵盤(pán)彈出時(shí)間

CGFloat

animaDur = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

 拿到鍵盤(pán)將要變化的y值

CGFloat

keyboardY = [note.userInfo[UIKeyboardFrameEndUserInfoKey]
CGRectValue].origin.y;

 計(jì)算出約束變化的值

CGFloat

bottomMargin = CGRectGetHeight([UIScreen mainScreen].bounds) - keyboardY;

self.bottomMar_Con.constant = bottomMargin;

 動(dòng)畫(huà)布局完成操作

[UIView

animateWithDuration:animaDur animations:^{

[self.view layoutIfNeeded];

}];

}

有添加就有移除

  • (void)dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容