我正在尝试使用objc_setAssociatedObject传递对象

我正在尝试使用以下代码传递Evento

Evento *evento = (Evento*)[secAtt objectAtIndex:indexPath.row];
objc_setAssociatedObject(evento, @"evento", self, OBJC_ASSOCIATION_RETAIN);


然后,我尝试使用以下代码获取此evento

Evento *evento = objc_getAssociatedObject(self, "evento");


返回nil

编辑:更多代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    NSString *sectionTitle = [sectionsTitles objectAtIndex:indexPath.section];
    NSArray *secAtt = [attractions objectForKey:sectionTitle];

    Evento *evento = (Evento*)[secAtt objectAtIndex:indexPath.row];
    objc_setAssociatedObject(self, @"evento", evento, OBJC_ASSOCIATION_RETAIN);

    NSString *tipoEntretenimento = evento.entretenimento.tipo.descricao;
    NSLog(@"tipo %@", tipoEntretenimento);
    UIImageView *imgCategoria = (UIImageView *)[cell.contentView viewWithTag:30];
   //[imgCategoria setContentMode:UIViewContentModeScaleAspectFit];
    if([tipoEntretenimento isEqualToString:@"Bares"])
        [imgCategoria setImage:[UIImage imageNamed:@"bares.png"]];
    else if ([tipoEntretenimento isEqualToString:@"Crianca"])
        [imgCategoria setImage:[UIImage imageNamed:@"crianca.png"]];
    else if ([tipoEntretenimento isEqualToString:@"Guloseimas"])
        [imgCategoria setImage:[UIImage imageNamed:@"guloseimas.png"]];
    else if ([tipoEntretenimento isEqualToString:@"Restaurantes"])
        [imgCategoria setImage:[UIImage imageNamed:@"restaurantes.png"]];
    else if ([tipoEntretenimento isEqualToString:@"Teatro"])
        [imgCategoria setImage:[UIImage imageNamed:@"teatro.png"]];
    else if ([tipoEntretenimento isEqualToString:@"Shows"])
        [imgCategoria setImage:[UIImage imageNamed:@"shows"]];
    else if ([tipoEntretenimento isEqualToString:@"Dança"])
        [imgCategoria setImage:[UIImage imageNamed:@"danca.png"]];
    else if ([tipoEntretenimento isEqualToString:@"Concertos"])
        [imgCategoria setImage:[UIImage imageNamed:@"concerto.png"]];
    else if ([tipoEntretenimento isEqualToString:@"Cinema"])
        [imgCategoria setImage:[UIImage imageNamed:@"cinema.png"]];


    UIFont *roboto = [UIFont fontWithName:@"Roboto-Bold" size:16];
    UILabel *lblNomeEvento = (UILabel *)[cell.contentView viewWithTag:10];
    lblNomeEvento.text = evento.entretenimento.nome;
    lblNomeEvento.font = roboto;

    Agenda *agenda = nil;
    if([evento.listaAgendas count] > 0)
        agenda = (Agenda *)[evento.listaAgendas objectAtIndex:0];

    NSLog(@"agenda %@", agenda.description);

    UIImageView *imgevento = (UIImageView *)[cell.contentView viewWithTag:20];
    [imgevento setContentMode:UIViewContentModeScaleAspectFit];
    [imgevento setImage:[UIImage imageNamed:@"pin.png"]];

    UIButton *btnCompartilhar = (UIButton *)[cell.contentView viewWithTag:40];
    [btnCompartilhar.titleLabel setHidden:YES];
    btnCompartilhar.titleLabel.text = evento.entretenimento.nome;
    [btnCompartilhar addTarget:self action:@selector(compartilharClick:) forControlEvents:UIControlEventTouchDown];


    UIButton *btnFavoritar = (UIButton *)[cell.contentView viewWithTag:50];
    [btnFavoritar addTarget:self action:@selector(favoritarClick:) forControlEvents:UIControlEventTouchDown];
    return cell;
}


后来当我尝试获取对象时:

-(void) compartilharClick:(id)sender {
    Evento *evento = (Evento *)objc_getAssociatedObject(self, "evento");

    UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Compartilhar:" delegate:self cancelButtonTitle:@"Cancelar" destructiveButtonTitle:nil otherButtonTitles:
                            @"Facebook",
                            @"Google+",
                            @"Twitter",
                            @"Outros",
                            nil];
    popup.tag = 1;
    [popup showInView:[UIApplication sharedApplication].keyWindow];
}

最佳答案

这里的问题可能是因为当函数需要const void *key时,您正在使用NSString作为键

OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)

尝试在实现上方添加static char *EventToKey = "EventToKey";

然后将objc_getAssociatedObjectobjc_setAssociatedObject的用法更改为

objc_setAssociatedObject(self, EventToKey, evento, OBJC_ASSOCIATION_RETAIN);

objc_getAssociatedObject(self, EventToKey);

10-08 05:52