标签:
+ (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;
通知中心(NSNotificationCenter)提供了相应的方法来帮助发布通知
- (void)postNotification:(NSNotification*)notification; 发布一个notification通知,可在notification对象中设置通知的名称、通知发布者、额外信息等 - (void)postNotificationName:(NSString*)aName object:(id)anObject; 发布一个名称为aName的通知,anObject为这个通知的发布者 - (void)postNotificationName:(NSString*)aName object:(id)anObject userInfo:(NSDictionary*)aUserInfo; 发布一个名称为aName的通知,anObject为这个通知的发布者,aUserInfo为额外信息
通知中心(NSNotificationCenter)提供了方法来注册一个监听通知的监听器(Observer)
- (void)removeObserver:(id)observer;id对象不再监听任何对象 - (void)removeObserver:(id)observer name:(NSString*)aName object:(id)anObject;
- (void)dealloc { //[super dealloc]; 非ARC中需要调用此句 [[NSNotificationCenterdefaultCenter] removeObserver:self]; }
UIDeviceOrientationDidChangeNotification // 设备旋转 UIDeviceBatteryStateDidChangeNotification // 电池状态改变 UIDeviceBatteryLevelDidChangeNotification// 电池电量改变 UIDeviceProximityStateDidChangeNotification // 近距离传感器(比如设备贴近了使用者的脸部)
UIKeyboardWillShowNotification// 键盘即将显示 UIKeyboardDidShowNotification//键盘显示完毕 UIKeyboardWillHideNotification// 键盘即将隐藏 UIKeyboardDidHideNotification// 键盘隐藏完毕 UIKeyboardWillChangeFrameNotification// 键盘的位置尺寸即将发生改变 UIKeyboardDidChangeFrameNotification//键盘的位置尺寸改变完毕
UIKeyboardFrameBeginUserInfoKey// 键盘刚开始的frame UIKeyboardFrameEndUserInfoKey// 键盘最终的frame(动画执行完毕后) UIKeyboardAnimationDurationUserInfoKey// 键盘动画的时间 UIKeyboardAnimationCurveUserInfoKey// 键盘动画的执行节奏(快慢)
(比如A对象告诉D对象发生了什么事情, A对象传递数据给D对象)
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 // 拿到通知中心 4 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 5 //添加监听器 6 [center addObserver:self selector:@selector(plusClick:) name:@"plusClickNotification" object:nil]; 7 } 8 object:nil]----- 无论谁发布都可以 9 Observer:观察者 10 // 取消注册通知监听器 11 - (void)dealloc 12 { 13 [[NSNotificationCenter defaultCenter] removeObserver:self]; 14 }
- (void)plusClick:(NSNotification *)note { // 取出cell(通知的发布者) XMGWineCell *cell = note.object; // 计算总价 int totalPrice = self.totalPriceLabel.text.intValue + cell.wine.money.intValue; // 设置总价 self.totalPriceLabel.text = [NSString stringWithFormat:@"%d", totalPrice]; }
- (IBAction)plusClick { // 修改模型 self.wine.count++; // 修改数量label self.countLabel.text = [NSString stringWithFormat:@"%d", self.wine.count]; // 减号能点击 self.minusButton.enabled = YES; // 发布通知 [[NSNotificationCenter defaultCenter] postNotificationName:@"plusClickNotification" object:self]; } default: 默认
make by - LJW
标签:
原文地址:http://www.cnblogs.com/ljwios/p/5445985.html