学习IOS开发UI篇--NSNotificationCenter通知中心

  NSNotificationCenter 较之于 Delegate
可以实现更大的跨度的通信机制,可以为两个无引用关系的两个对象进行通信。NSNotificationCenter 的通信原理使用了观察者模式;

  1. NSNotificationCenter 注册观察者对某个事件(以字符串命名)感兴趣,及该事件触发时该执行的 Selector 或
Block

  2. NSNotificationCenter 在某个时机激发事件(以字符串命名)

  3. 观察者在收到感兴趣的事件时,执行相应的 Selector 或
Block

使用通知中心的步骤:

1.注册观察者


[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(selector)
name:@"NOTIFICATION_NAME"
object:nil];

2.激发事件(发布广播)


[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_NAME"
object:nil];

3. 观察者会自动执行selector方法

4.当不想监听的时候取消监听(务必在对象销毁的同时进行)


一般在监听器销毁之前取消注册(如在监听器中加入下列代码):
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

学习IOS开发UI篇--NSNotificationCenter通知中心

时间: 2024-08-29 09:38:53

学习IOS开发UI篇--NSNotificationCenter通知中心的相关文章

学习IOS开发UI篇--UIScrollView/delegate/pagecontrol/UITimer

1.UIscrollView的属性 ================================================== 1.1 常见属性 @property(nonatomic) CGPoint contentOffset; 这个属性用来表示UIScrollView滚动的位置 @property(nonatomic) CGSize contentSize; 这个属性用来表示UIScrollView内容的尺寸,滚动范围(能滚多远) @property(nonatomic) UIE

学习IOS开发UI篇--UI知识点总结(四) UITabelView/UITableViewCell

UITabelView:常用属性 @property (nonatomic)          CGFloat    rowHeight;             // will return the default value if unset @property (nonatomic)          CGFloat     sectionHeaderHeight;   // will return the default value if unset @property (nonatom

学习IOS开发UI篇--UI知识点总结(三) UIScrollView/UIPageControl/NSTimer

UIScrollView:常用属性 @property(nonatomic)   UIEdgeInsets     contentInset;               // default UIEdgeInsetsZero. add additional scroll area around content @property(nonatomic,getter=isPagingEnabled) BOOL   pagingEnabled;     // default NO. if YES,

学习IOS开发UI篇--UITableView/数据模型嵌套/UITableViewCell/Cell的重用

1.UITableView ================================================== UITableView有两种格式:group和plain 2.UITableView如何展示数据 ================================================== UITableView需要一个数据源(dataSource)来显示数据 凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的

学习IOS开发UI篇--数据存储

iOS应用数据存储的常用方式 1.lXML属性列表(plist)归档 2.lPreference(偏好设置) 3.lNSKeyedArchiver归档(NSCoding) 4.lSQLite3 5.lCore Data Documents:保存应用运行时生成的需要持久化的数据,iTunes同步设备时会备份该目录.例如,游戏应用可将游戏存档保存在该目录 tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除.应用没有运行时,系统也可能会清除该目录下的文件.iTunes同步设备时

学习IOS开发UI篇--UI知识点总结(一) UIButton/UITextField

UIkit框架下的几个基本控件,UIButton,UITextField,UILabel,UIImageView,UIScrollView,UITableView,UITableViewCell,UIPageControl; 他们的继承关系,UILabel,UIImageView,UIScrollView,UITableViewCell,直接继承自UIView; UIButton,UITextField,UIPageControl,继承自UIControl; UIControl继承自UIView

学习IOS开发UI篇--UIAlertView/UIActionSheet

UIAlertView,UIActionSheet都是系统的弹框 UIAlertView:创建方法 - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherBu

iOS开发UI篇章之通知中心(NSNotificationCenter)

一.通知中心(NSNotificationCenter) 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象 之间的消息通信 • 任何一个对象都可以向通知中心发布通知(NSNotification),描述自己在做什么.其他感 兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时) 二.通知(NSNotification) •一个完整的通知一般包含3个属性: Ø- (NSString *)name; // 通知的

学习IOS开发UI篇--UITabBarController

UITabBarController的使用步骤 1.初始化UITabBarController 2.设置UIWindow的rootViewController为UITabBarController 3.根据具体情况,通过addChildViewController方法添加对应个数的子控制器 UITabBarController添加控制器的方式有2种 - (void)addChildViewController:(UIViewController *)childController;  添加单个子