通知中心(NSNotificationCenter)
通知(NSNotification)
一個完整的通知一般包含3個屬性:(注意順序)
(NSString*)name; 通知的名稱
(id)object; 通知發(fā)布者(是誰要發(fā)布通知)
(NSDictionary *)userInfo; 一些額外的信息(通知發(fā)布者傳遞給通知接收者的信息內(nèi)容)
初始化(可以理解為創(chuàng)建)一個通知(NSNotification)對象
只有通知的的名稱和通知的發(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)的方法來幫助發(fā)布通知
-(void)postNotification:(NSNotification *)notification;
發(fā)布一個notification通知刊橘,可在notification對象中設(shè)置通知的名稱锦募、通知發(fā)布者粤攒、額外信息等
-(void)postNotificationName:(NSString *)aName object:(id)anObject;
發(fā)布一個名稱為aName的通知,anObject為這個通知的發(fā)布者
-(void)postNotificationName:(NSString *)aName object:(id)anObject
userInfo:(NSDictionary *)aUserInfo;
發(fā)布一個名稱為aName的通知坪郭,anObject為這個通知的發(fā)布者,aUserInfo為額外信息
1.開始監(jiān)聽意思是(當Bob監(jiān)聽到了baby2發(fā)出的娛樂的通知,就會調(diào)用eat這個方法)
[[NSNotificationCenter defaultCenter] addObserver:Bob selector:@selector(eat:) name:@"娛樂" object:baby2];
2.創(chuàng)建通知(baby2創(chuàng)建了一個叫娛樂的通知,并設(shè)置了通知的內(nèi)容)
NSNotification*note = [NSNotification notificationWithName:@"娛樂" object:baby2 userInfo:@{@"通知的key":@"通知的value"}];
3.發(fā)布通知(三部曲)
[[NSNotificationCenter defaultCenter] postNotification:note];
第二種簡寫方式
[[NSNotificationCenter defaultCenter] addObserver:baby selector:@selector(eat:) name:@"通知的名稱" object:baby2];
[[NSNotificationCenter
defaultCenter] postNotificationName:@"通知的名稱"
object:baby2 userInfo:@{@"通知的key":@"通知的value"}];
注冊通知監(jiān)聽器
通知中心(NSNotificationCenter)提供了方法來注冊一個監(jiān)聽通知的監(jiān)聽器(Observer)
-(void)addObserver:(id)observer
selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
observer:監(jiān)聽器苏研,即誰要接收這個通知
aSelector:收到通知后瞭恰,回調(diào)監(jiān)聽器的這個方法,并且把通知對象當做參數(shù)傳入
aName:通知的名稱瓢喉。如果為nil宁赤,那么無論通知的名稱是什么,監(jiān)聽器都能收到這個通知
anObject:通知發(fā)布者栓票。如果為anObject和aName都為nil决左,監(jiān)聽器都收到所有的通知
-(id)addObserverForName:(NSString *)name object:(id)obj
queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification
*note))block;
name:通知的名稱
obj:通知發(fā)布者
block:收到對應(yīng)的通知時,會回調(diào)這個block
queue:決定了block在哪個操作隊列中執(zhí)行走贪,如果傳nil佛猛,默認在當前操作隊列中同步執(zhí)行
取消注冊通知監(jiān)聽器
通知中心不會保留(retain)監(jiān)聽器對象,在通知中心注冊過的對象坠狡,必須在該對象釋放前取消注冊继找。否則,當相應(yīng)的通知再次出現(xiàn)時逃沿,通知中心仍然會向該監(jiān)聽器發(fā)送消息婴渡。因為相應(yīng)的監(jiān)聽器對象已經(jīng)被釋放了,所以可能會導(dǎo)致應(yīng)用崩潰
通知中心提供了相應(yīng)的方法來取消注冊監(jiān)聽器
-(void)removeObserver:(id)observer;
-(void)removeObserver:(id)observer name:(NSString
*)aName object:(id)anObject;
一般在監(jiān)聽器銷毀之前取消注冊(如在監(jiān)聽器中加入下列代碼):
-(void)dealloc {
[super dealloc]; 非ARC中需要調(diào)用此句
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
UIDevice通知
UIDevice類提供了一個單粒對象感挥,它代表著設(shè)備缩搅,通過它可以獲得一些設(shè)備相關(guān)的信息,比如電池電量值(batteryLevel)触幼、電池狀態(tài)(batteryState)骨田、設(shè)備的類型(model优质,比如iPod、iPhone等)咙咽、設(shè)備的系統(tǒng)(systemVersion)
通過[UIDevice currentDevice]可以獲取這個單粒對象(一般用來做系統(tǒng)適配)
UIDevice對象會不間斷地發(fā)布一些通知亿傅,下列是UIDevice對象所發(fā)布通知的名稱常量:
UIDeviceOrientationDidChangeNotification 設(shè)備旋轉(zhuǎn)
UIDeviceBatteryStateDidChangeNotification 電池狀態(tài)改變
UIDeviceBatteryLevelDidChangeNotification 電池電量改變
UIDeviceProximityStateDidChangeNotification 近距離傳感器(比如設(shè)備貼近了使用者的臉部)
鍵盤通知
我們經(jīng)常需要在鍵盤彈出或者隱藏的時候做一些特定的操作,因此需要監(jiān)聽鍵盤的狀態(tài)
鍵盤狀態(tài)改變的時候,系統(tǒng)會發(fā)出一些特定的通知
UIKeyboardWillShowNotification 鍵盤即將顯示
UIKeyboardDidShowNotification 鍵盤顯示完畢
UIKeyboardWillHideNotification 鍵盤即將隱藏
UIKeyboardDidHideNotification 鍵盤隱藏完畢
UIKeyboardWillChangeFrameNotification 鍵盤的位置尺寸即將發(fā)生改變
UIKeyboardDidChangeFrameNotification 鍵盤的位置尺寸改變完畢
系統(tǒng)發(fā)出鍵盤通知時,會附帶一下跟鍵盤有關(guān)的額外信息(字典),字典常見的key如下:
UIKeyboardFrameBeginUserInfoKey 鍵盤剛開始的frame
UIKeyboardFrameEndUserInfoKey 鍵盤最終的frame(動畫執(zhí)行完畢后)
UIKeyboardAnimationDurationUserInfoKey 鍵盤動畫的時間
UIKeyboardAnimationCurveUserInfoKey 鍵盤動畫的執(zhí)行節(jié)奏(快慢)
-
(void)viewDidLoad {
[super viewDidLoad];
// 即將顯示的鍵盤
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];// 即將推出的鍵盤
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
/**
- 監(jiān)聽鍵盤的即將顯示
*/
- (void)keyboardWillShow:(NSNotification *)note
{
// 獲得鍵盤的frame
CGRectframe = [note.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
// 修改底部約束
self.bottn.constant= frame.size.height;
執(zhí)行動畫
CGFloatduration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
/**
- 監(jiān)聽鍵盤的即將隱藏
*/
- (void)keyboardWillHide:(NSNotification *)note
{
// 修改底部約束
self.bottn.constant= 0;
// 執(zhí)行動畫
CGFloatduration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 文本框不再是第一響應(yīng)者媒峡,就會退出鍵盤
[self.textField resignFirstResponder];
[self.textField endEditing:YES];
[self.view endEditing:YES];
}
注意:監(jiān)聽鍵盤的顯示和隱藏,最重要的思想就是拿整個view的高度減去鍵盤的高度,注意細節(jié)
通知和代理的選擇
共同點
利用通知和代理都能完成對象之間的通信
(比如A對象告訴D對象發(fā)生了什么事情, A對象傳遞數(shù)據(jù)給D對象)
不同點
代理 :一對一關(guān)系(1個對象只能告訴另1個對象發(fā)生了什么事情)
通知
:
多對多關(guān)系(1個對象能告訴N個對象發(fā)生了什么事情, 1個對象能得知N個對象發(fā)生了什么事情)
****************************************************************************************************************************************筆記**********************************************
框架搭建
框架搭建
- 1.操作storyboard(toolBar+tableView)
- 2.添加資料(plist,圖片,MJExtension)
3.創(chuàng)建對應(yīng)模型/cell(xib注冊)
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動態(tà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卡頓,記得寫這兩行代碼
self.layer.shouldRasterize =YES;
self.layer.rasterizationScale =YES;
NSNotification
NSNotification
實例中的用法
先定義通知宏(const常量字符串)
- 命名規(guī)范:所在類+觸發(fā)發(fā)送的行為+Notification
define XMGWineCellPlusOnClickNotification @"XMGWineCellPlusOnClickNotification"
define XMGWineCellMinusOnClickNotification @"XMGWineCellMinusOnClickNotification"
define XMGWineCellMinusWine @"XMGWineCellMinusWine"
發(fā)送通知
- 通知的發(fā)送一般都有觸發(fā)條件,比如按鈕的點擊
減號按鈕點擊發(fā)出的通知
[[NSNotificationCenter
defaultCenter] postNotificationName:XMGWineCellMinusOnClickNotification object:nil userInfo:@{XMGWineCellMinusWine:self.wine}];
加號按鈕點擊發(fā)出的通知
[[NSNotificationCenter
defaultCenter] postNotificationName:XMGWineCellPlusOnClickNotification object:self];
添加通知監(jiān)聽
- 一般是在控制器的viewDidLoad/viewDidAppear等方法中/或者自定義view的initWithFrame/Corder方法中添加
-
(void)viewDidLoad {
[super viewDidLoad];
讓控制器成為監(jiān)聽者
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(cellPlusBtnOnClick:)
name:XMGWineCellPlusOnClickNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(cellMinusBtnOnClick:)
name:XMGWineCellMinusOnClickNotification object:nil];
}
監(jiān)聽者接收通知后調(diào)用方法
- 在收到通知后才會觸發(fā)調(diào)用
pragma
mark - NotificationSEL
- (void)cellPlusBtnOnClick:(NSNotification *)note
{
[self.tableView reloadData];
XMGWine
*wine = [(XMGWineCell *)note.object wine];
如果不包含這個模型先添加進來
if (![self.winesInCar
containsObject:wine]) {
[self.winesInCar addObject:wine];
}
計算應(yīng)該顯示的總價格
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];
如果購物車模型中的購買數(shù)量是0,那就移除
if (wine.count == 0 && [self.winesInCar containsObject:wine]) {
[self.winesInCar removeObject:wine];
}
計算應(yīng)該顯示的總價格
CGFloat
totalMoney = self.totalMoney_Label.text.doubleValue;
totalMoney -= wine.money.doubleValue;
self.totalMoney_Label.text = [NSString
stringWithFormat:@"%.1f",
totalMoney];
}
移除監(jiān)聽者
- 有添加必須以后移除
有添加就需要移除
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
系統(tǒng)自帶通知的用法
- 只監(jiān)聽移除即可
- 例如cell中監(jiān)聽文本框文字變化
-
(void)awakeFromNib {
Initialization code
[[NSNotificationCenter defaultCenter] addObserver:self
讓cell監(jiān)聽通知
selector:@selector(textChange:) 收到通知后調(diào)用的方法
name:UITextFieldTextDidChangeNotification 通知的名稱
object:self.buyCount_TF];
發(fā)出通知的對象
}
文字改變的通知
-
(void)textChange:(NSNotification *)note {
NSLog(@"%s, line = %d, note = %@",FUNCTION, LINE, note);
此處只是演示作用,具體
1.攔截判斷
2.改變總價格
根據(jù)當前模型中的buyCount與當前值對比,調(diào)用相應(yīng)次數(shù)的代理方法
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Delegate
delegate
代理的使用步驟
定義一份代理協(xié)議
協(xié)議名字的格式一般是:類名+ Delegate
比如UITableViewDelegate
代理方法細節(jié)
一般都是@optional
方法名一般都以類名開頭
比如-
(void)scrollViewDidScroll:一般都需要將對象本身傳出去
比如tableView的方法都會把tableView本身傳出去
必須要遵守NSObject協(xié)議
比如@protocol
XMGWineCellDelegate <NSObject>聲明一個代理屬性
代理的類型格式:id<協(xié)議>
delegate
@property (nonatomic,
weak) id<XMGWineCellDelegate>
delegate;
- 設(shè)置代理對象
xxx.delegate = yyy;
- 代理對象遵守協(xié)議谅阿,實現(xiàn)協(xié)議里面相應(yīng)的方法
參考tableView的delegate三大步驟
當控件內(nèi)部發(fā)生了一些事情,就可以調(diào)用代理的代理方法通知代理
如果代理方法是@optional酬滤,那么需要判斷方法是否有實現(xiàn)
if ([self.delegate
respondsToSelector:@selector(wineCell:didClicked:)])
{
[self.delegate wineCell:self
didClicked:XMGWineCellCalculatTypeMinus];
}
iOS監(jiān)聽某些事件的方法
通知(NSNotificationCenter\NSNotification)
任何對象之間都可以傳遞消息
使用范圍
1個對象可以發(fā)通知給N個對象
1個對象可以接受N個對象發(fā)出的通知
必須得保證通知的名字在發(fā)出和監(jiān)聽時是一致的
KVO
僅僅是能監(jiān)聽對象屬性的改變(靈活度不如通知和代理)
代理
使用范圍
1個對象只能設(shè)置一個代理(假設(shè)這個對象只有1個代理屬性)
1個對象能成為多個對象的代理
比
通知
規(guī)范建議使用優(yōu)先級代理->通知
UIAlertController
UIAlertController
- 來源于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判斷是哪個按鈕點擊了
}
- 不必再設(shè)置代理
- (void)testAlertController {
是用來替代UIAlertView/UIActionSheet
1.創(chuàng)建提示框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"alertController" 標題
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(鍵盤處理):
keyBoardEditing
pragma mark - 鍵盤處理
-
(void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = 80;
監(jiā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}}";
*/
拿到鍵盤彈出時間
CGFloat
animaDur = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
拿到鍵盤將要變化的y值
CGFloat
keyboardY = [note.userInfo[UIKeyboardFrameEndUserInfoKey]
CGRectValue].origin.y;
計算出約束變化的值
CGFloat
bottomMargin = CGRectGetHeight([UIScreen mainScreen].bounds) - keyboardY;
self.bottomMar_Con.constant = bottomMargin;
動畫布局完成操作
[UIView
animateWithDuration:animaDur animations:^{
[self.view layoutIfNeeded];
}];
}
有添加就有移除
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}