如何发送&使用iOS中的NSNotificationCenter接收数据(XCode6.4)

时间:2015-08-21 11:38:53

标签: ios objective-c

我正面临NSNotificationCenter的问题。

我无法在最新的ios 8.4(XCode 6.4)中使用NSNotificationCenter发送消息和接收消息

请检查以下代码:

1)我想使用第一个视图控制器将数据发送到另一个视图。 所以我在第一个viewcontroller中编写了以下代码:

当用户btn单击方法时如下:

- (IBAction)btnClicked:(id)sender 
{ 
  [self postNotification];
  [self performSegueWithIdentifier:@"asGo" sender:self]; 
} 

-(void)postNotification{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];
}

2)在第二个视图控制器中,我在ViewWillApper中添加了观察者,如下所示:

-(void) viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:YES];

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


-(void)eventListenerDidReceiveNotification:(NSNotification*)txt
{
 NSLog(@"i got notfication:");
} 

因此在查看时不会调用eventListenerDidReceiveNotification。 但是当我带着导航第二个vc时,我没有登上日志

3 个答案:

答案 0 :(得分:0)

NSNotificationCenter基本上有3个步骤

  • 添加[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(open:) name:@"OpenDetail" object:nil];
  • 等观察者
  • 发布通知[[NSNotificationCenter defaultCenter] postNotificationName:@"OpenDetail" object:self];
  • 删除观察者[[NSNotificationCenter defaultCenter] removeObserver:self name:@"OpenDetail" object:nil];

我认为您发布了通知,然后添加了观察者,反之亦然。您必须先添加观察者然后发布通知。 HTH

答案 1 :(得分:0)

首先,您必须设置要发送的数据

NSDictionary *userInfo = [NSDictionary dictionaryWithObject:myObject forKey:@"aKey"];

然后你用这样的数据发布它:

[[NSNotificationCenter defaultCenter] postNotificationName: @"MyNotification" object:nil userInfo:userInfo];

最后你读了通知中的数据:

-(void)eventListenerDidReceiveNotification:(NSNotification*)notification
{
 NSLog(@"i got notification:");
 NSDictionary *userInfo = notification.userInfo;
 NSString *myObject = [userInfo objectForKey:@"aKey"];
} 

答案 2 :(得分:0)

正如其他人所说,NSNotificationCenter不像邮局那样有效。它只会在有人在他们到达时实际收听通知时发送通知。这就是你没有调用eventListenerDidReceiveNotification方法的原因:你在viewWillAppear中添加了一个观察者,它在segue之后被调用(我假设你因为performSegueWithIdentifier而使用了segues你的代码中的方法已经完成,因此在调用postNotification之后肯定会调用它。

因此,为了通过NSNotificationCenter传递数据,您必须在发布通知之前添加观察者 。 以下代码完全无用且不必要地过于复杂,你不应该做那样的事情,但是因为你一直坚持使用这样的方案,所以你去:

//Didn't test this code. Didn't even compile it, to be honest, but it should be enough to get the idea.

NSString * const SOUselessNotificationName = @"MyUselessNotification";

#pragma mark - FIRST VC

@interface SOFirstVC : UIViewController
@end

@implementation SOFirstVC

NSString * const SOasGoSegueIdentifer = @"asGo";

- (IBAction)btnClicked:(id)sender { 
    [self performSegueWithIdentifier:SOasGoSegueIdentifer sender:self]; 
}

-(void)postNotification {
    [[NSNotificationCenter defaultCenter] postNotificationName:SOUselessNotificationName object:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifer isEqualToString:SOasGoSegueIdentifer]) {
        SOSecondVC *destinationVC = (SOSecondVC *)segue.destinationViewController;

        [destinationVC registerToReceiveNotificationsFromObject:self];
        [self postNotification];
    }
}

@end

#pragma mark - SECOND VC

@interface SOSecondVC : UIViewController

-(void)registerToReceiveNotificationsFromObject:(id)object;

@end

@implementation SOSecondVC

-(void)registerToReceiveNotificationsFromObject:(id)object {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:(eventListenerDidReceiveUselessNotification:) name:SOUselessNotificationName object:object];
}

-(void)eventListenerDidReceiveUselessNotification:(NSNotification*)uselessNotification {
 NSLog(@"I got a useless notfication! Yay!");
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end