NSMutableArray 通知

Notification with NSMutableArray

我的代码:

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
}

我想在我的 VC 中收到通知,然后将对象添加到 temporaryArray,然后将其添加到 playersSelected 中。怎么做最好?

您可以通过通知来完成,但我认为您应该使用 delegate

在您的 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];

在你的 VC.m 中:(viewDidLoad)

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

- (void)handleDidChangePlayerList:(NSNotification*)notification {
    NSDictionary *userInfo = notification.userInfo;

    [self.temporaryArray addObject:[userInfo valueForKey:@"articleId"]];
}