Search code examples
objective-cuitableviewnsmutablearraynsnotificationcenter

Notification with NSMutableArray


My code :

popover.m

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.playersSelected addObject:[self.players objectAtIndex:indexPath.row]];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didChangePlayerList:)
                                                 name:@"sth"
                                               object:self.playersSelected];

}

VC.m

-(void)didchangePlayerList:(NSNotification *)notification {

    self.temporaryArray
}

I want to get notify i my VC and add object to temporaryArray then when it's added in playersSelected. How to do it best?


Solution

  • You can do it with notification but I think you should use a delegate.

    In your popover.m : (didSelectRowAtIndexPath)

    [self.playersSelected addObject:[self.players objectAtIndex:indexPath.row]];
    
    NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
    [userInfo setObject:[self.players objectAtIndex:indexPath.row] forKey:@"object"];
    
    // Send parameters with userInfo
    [[NSNotificationCenter defaultCenter] postNotificationName:@"didChangePlayerList" object:nil userInfo:userInfo];
    

    In your VC.m : (viewDidLoad)

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

    and

    - (void)handleDidChangePlayerList:(NSNotification*)notification {
        NSDictionary *userInfo = notification.userInfo;
    
        [self.temporaryArray addObject:[userInfo valueForKey:@"articleId"]];
    }