本文的结构是这样的:先是回答博友的一个关于NSNotification的问题,然后将相关的知识点详细介绍下。【普及文,大神绕行】
(如果你在开发中遇到解决不了的技术问题,可以考虑和我交流下!iOS两年高强度开发中,喜欢收集各种问题!右侧有我QQ微博。
//http://www.cnblogs.com/ChenYilong/)
博友的问题如下所示:
有三块代码,其中1和3断点可进入,2进不去
1.
- (id)init
{
XIBLog(@"- (id)init");
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popToTabbarController) name:@"popToTabbarController"object:nil];
self.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[XIBIngViewController alloc] init]];
[self.rootViewController setNavigationBarHidden:YES];
}
return self;
}
2.
- (void)popToTabbarController
{
XIBLog(@"- (void)popToTabbarController:(NSNotification *)notification");
self.tabBarController = [[XIBTabBarController alloc] init];
[self.rootViewController pushViewController:self.tabBarController animated:YES];
}
3.
- (void)viewDidLoad
{
XIBLog(@"加载完毕");
XIBLog(@"程序启动时显示的欢迎界面,界面内容可变、节假日时切换或者放置广告");
XIBLog(@"- (void)viewDidLoad");
[super loadView];
[self checkNetwork];
[self getIPhoneInformation];
[self getServerInformation];
[self upGrade];
[self checkMenu];
[self checkLanguage];
[self firstRun];
[self checkSessionCookie];
[[NSNotificationCenter defaultCenter] postNotificationName:@"popToTabbarController" object:nil];
[super viewDidLoad];
}
回答如下:
将3中代码更改如下:
[[NSNotificationCenter defaultCenter] postNotificationName:@"popToTabbarController" object:nil];
改成
[[NSNotificationCenter defaultCenter] postNotificationName:@"popToTabbarController" object:self userInfo:nil];
下面相关知识普及如下:
NSNotification通知
//http://www.cnblogs.com/ChenYilong/
在IOS中,每一个运行中的程序都有个NSNotificationCenter(通知中心)成员变量。如果有对象需要关注某一个NSNotification(通知),
首先去通知中心注册
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(todoOne) name:@"Method" object:nil];
其中self就是关注某一通知的对象,也叫Observer;@"Method"指的是Notification的名字;@selector里面的方法todoOne就是这个对象接受到通知(Method)的时候该执行的动作;最后一个参数指的是发送通知的对象,如果发送通知的对象为nil,则通知中心将所有名字为@“Method”的NSNotification转发给关注这个通知的对象self。
其次,在需要的时候通知中心发送通知,这个时候,关注Method的对象就会调用todoOne方法。
[[NSNotificationCenter defaultCenter] postNotificationName:@"Method" object:self];
最后移除关注Method的对象
[[NSNotificationCenter defaultCenter] removeObserver:self];
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(todoOne) name:@"Method"object:nil];
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(todoTwo:) name:@"two"object:nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)todoOne{
NSLog(@"todoOne is called");
}
- (void)todoTwo:(NSNotification *)object{
NSLog(@"todoTwo: is called %@",object);
NSNumber *integer = [[object userInfo] objectForKey:@"sum"];
NSLog(@"the received is %d",[integer intValue]);
}
- (IBAction)sendNotification1:(id)sender {
[[NSNotificationCenterdefaultCenter] postNotificationName:@"Method"object:self];
}
- (IBAction)sendNotification2:(id)sender {
NSDictionary *mDictionary = [NSDictionarydictionaryWithObject:[NSNumbernumberWithInteger:5] forKey:@"sum"];
[[NSNotificationCenterdefaultCenter] postNotificationName:@"two"object:selfuserInfo:mDictionary];
}
- (IBAction)removeNotification:(id)sender {
[[NSNotificationCenterdefaultCenter] removeObserver:self];
NSLog(@"remove Notification");
}
@end
NSNotification类
提供给observer的信息包裹. notification对象有两个重要的成员变量: name 和 object.
- (NSString *)name;
- (id)object;
- (NSDictionary *)userInfo;我们想要notification对象传递更多的信息
+ (id)notificationWithName:(NSString *)aName object:(id)anObject;
+ (id)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
NSNotificationCenter类
+ (id)defaultCenter;返回notification center [类方法,返回全局对象, 单件模式.cocoa的很多的全局对象都是通过类似方法实现]
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
如果notificationName为nil. 那么notification center将anObject发送的所有notification转发给observer
. 如果anObject为nil.那么notification center将所有名字为notificationName的notification转发给observer
- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(id)anObject;
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
参考博客:
http://blog.sina.com.cn/s/blog_5df7dcaf0100c0q2.html
http://blog.csdn.net/ch_soft/article/details/6682809
http://m.oschina.net/blog/94253
//http://www.cnblogs.com/ChenYilong/
(如果你在开发中遇到解决不了的技术问题,可以考虑和我交流下!iOS两年高强度开发中,喜欢收集各种问题!右侧有我QQ微博。
//http://www.cnblogs.com/ChenYilong/)