How to pass prepareForSegue: an object

后端 未结 10 2056
北荒
北荒 2020-11-21 11:18

I have many annotations in a mapview (with rightCalloutAccessory buttons). The button will perform a segue from this mapview to a tableview

10条回答
  •  隐瞒了意图╮
    2020-11-21 11:40

    I have a sender class, like this

    @class MyEntry;
    
    @interface MySenderEntry : NSObject
    @property (strong, nonatomic) MyEntry *entry;
    @end
    
    @implementation MySenderEntry
    @end
    

    I use this sender class for passing objects to prepareForSeque:sender:

    -(void)didSelectItemAtIndexPath:(NSIndexPath*)indexPath
    {
        MySenderEntry *sender = [MySenderEntry new];
        sender.entry = [_entries objectAtIndex:indexPath.row];
        [self performSegueWithIdentifier:SEGUE_IDENTIFIER_SHOW_ENTRY sender:sender];
    }
    
    -(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:SEGUE_IDENTIFIER_SHOW_ENTRY]) {
            NSAssert([sender isKindOfClass:[MySenderEntry class]], @"MySenderEntry");
            MySenderEntry *senderEntry = (MySenderEntry*)sender;
            MyEntry *entry = senderEntry.entry;
            NSParameterAssert(entry);
    
            [segue destinationViewController].delegate = self;
            [segue destinationViewController].entry = entry;
            return;
        }
    
        if ([[segue identifier] isEqualToString:SEGUE_IDENTIFIER_HISTORY]) {
            // ...
            return;
        }
    
        if ([[segue identifier] isEqualToString:SEGUE_IDENTIFIER_FAVORITE]) {
            // ...
            return;
        }
    }
    

提交回复
热议问题