zoukankan      html  css  js  c++  java
  • 【问题收集·初级】回答博客园博友的NSNotification问题【普及文,大神绕行】

     

    本文的结构是这样的:先是回答博友的一个关于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.

        [[NSNotificationCenterdefaultCenteraddObserver:selfselector:@selector(todoOne) name:@"Method"object:nil];

        [[NSNotificationCenterdefaultCenteraddObserver: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 userInfoobjectForKey:@"sum"];

        NSLog(@"the received is %d",[integer intValue]);

        

    }

     

    - (IBAction)sendNotification1:(id)sender {

        [[NSNotificationCenterdefaultCenterpostNotificationName:@"Method"object:self];

    }

     

    - (IBAction)sendNotification2:(id)sender {

        NSDictionary *mDictionary = [NSDictionarydictionaryWithObject:[NSNumbernumberWithInteger:5forKey:@"sum"];

        [[NSNotificationCenterdefaultCenterpostNotificationName:@"two"object:selfuserInfo:mDictionary];

    }

    - (IBAction)removeNotification:(id)sender {

        [[NSNotificationCenterdefaultCenterremoveObserver: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;

     如果notificationNamenil那么notification centeranObject发送的所有notification转发给observer

    如果anObjectnil.那么notification center将所有名字为notificationNamenotification转发给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/

     

     

     

  • 相关阅读:
    ES6关于Promise的用法
    JS进阶篇--JS数组reduce()方法详解及高级技巧
    JavaScript常用数组操作方法,包含ES6方法
    揭密 Vue 的双向绑定
    JavaScript(E5,6) 正则学习总结学习,可看可不看!
    利用scons构建project
    cuda核函数再调用核函数,多层并行
    使用微信JSSDK实现图片上传
    android 自己定义水平和圆形progressbar 仅仅定义一些style就能够
    [LeetCode] 035. Search Insert Position (Medium) (C++)
  • 原文地址:https://www.cnblogs.com/ChenYilong/p/3751905.html
Copyright © 2011-2022 走看看