0

I have this really unexplained problem with NSNotification method.

I have been using NSNotificationCenter for a long time but i can't explain why this is happening.

My problem is this,

I have a UITableViewCell subclass where i send a NSNotificationCenter method to the UIViewController when a user taps a button in the cell.

[[NSNotificationCenter defaultCenter] postNotificationName:MOVE_TO_PROGRAM_VIEW
                                                    object:self
                                                  userInfo:@{INDEX_ROW : [NSNumber numberWithInteger:self.tag]}];

Where the self.tag is the row (for the data model in the controller).

In the controller i register for the notification in viewWillAppear: like so:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userWantsToGoToProgramView:) name:MOVE_TO_PROGRAM_VIEW object:nil];

I also remove myself in the viewWillDisappear:

[[NSNotificationCenter defaultCenter] removeObserver:self name:MOVE_TO_PROGRAM_VIEW object:nil];

Now in the method for the notification i try to get the userInfo and the row but the notificaiton argument is nil for some reason..

- (void)userWantsToGoToProgramView:(NSNotification *)notification
{
    // notification is nil here


    // get the index of the video in the feed
    NSDictionary *userInfo = notification.userInfo;
    NSInteger videoIndex = [userInfo[INDEX_ROW] integerValue];
    NSDictionary *videoData = self.feed[videoIndex];

}

Any advice of help will be appreciated

Thanks!

YYfim
  • 1,402
  • 1
  • 9
  • 24
  • I can't explain your problem, but why are you even using a notification for this purpose? Why not use the button's action method directly? You can get the indexPath of the row the button was in using indexPathForRowAtPoint:, or using the button's tag. – rdelmar Apr 13 '15 at 15:51
  • This is a subclass of uitableviewcell. And I need to let the uiviewcontroller that a button in a this cell was pressed so I can segue to another view controller. But that is not the main issue, this specific problem could be solved with a few other approaches. I do want to know why is this happening (I have also created another Notificationcenter method that don't work also, this means I have a problem is the apps architecture somewhere..) – YYfim Apr 13 '15 at 16:01
  • How are you determining that the notification is nil? It makes no sense that notification could be nil but notification.userInfo is not nil. – rdelmar Apr 13 '15 at 16:34
  • I have placed a breakpoint when the callback method gets called and it is nil – YYfim Apr 13 '15 at 16:57
  • 2
    OK, sometimes the debugger gives spurious results. If you log notification, I'm sure you'll find that it is not nil. – rdelmar Apr 13 '15 at 17:12
  • Hi, i've logged the notification and found out it is of type NSConcreteNotification, i found it is a private class of NSNotificaiton that i'm not spouse to get.. – YYfim Apr 14 '15 at 04:34
  • NSNotification is a class cluster, so the fact that you get a NSConcreteNotification is fine. That's what I get too. Don't worry about it. – rdelmar Apr 14 '15 at 04:54
  • ohh ok, but for some reason the "userInfo" is empty there – YYfim Apr 14 '15 at 04:55
  • You said in your question that you got the useInfo, so now you're saying that you don't? – rdelmar Apr 14 '15 at 04:56
  • no, sorry my writing is a bit misleading i'll edit that. The user info is nil – YYfim Apr 14 '15 at 04:58

0 Answers0