Chris Matthews
Chris Matthews

Reputation: 382

Pass sender of programmatically called segue to destination

Sorry if the title isn't very clear, but hopefully I can elaborate here.

I have a ViewController MatchLineupViewController, which displays 22 buttons to represent rugby players on a team. When the user taps any of these buttons, a modal segue is called programmatically in the following method:

- (IBAction) showSquadSelector:(UIButton *)sender {
    [self performSegueWithIdentifier:@"SeguePopupSquad" sender:sender];
}

The modal ViewController which is then displayed is called SquadSelectViewController. It passes back a selected player object to the MatchLineupViewController, which is acting as a delegate. This works perfectly.

However, I want to assign the profile_picture attribute of the returned object to the UIButton that sent the segue in the first place.

EDIT - The returned object is an NSDictionary as shown in the following code:

- (void) selectPlayer:(NSDictionary *)player forButton:(UIButton *)sender {
    [sender.imageView setImage:[UIImage imageWithContentsOfFile:[player objectForKey:@"profile_picture"]]];
}

How would I go about doing this? If you require any further code to understand what I am asking, I can provide it.

Many thanks, Chris

EDIT -

- (IBAction) showSquadSelector:(UIButton *)sender {
    [self performSegueWithIdentifier:@"SeguePopupSquad" sender:sender];
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"SeguePopupSquad"]) {
        SquadSelectViewController *ssvc = (SquadSelectViewController *) segue.destinationViewController;
        ssvc.myDelegate = self;
        ssvc.senderButton = sender;
    }
}

- (void) selectPlayer:(NSDictionary *)player forButton:(UIButton *)sender {
    [sender.imageView setImage:[UIImage imageWithContentsOfFile:[player objectForKey:@"profile_picture"]]];
    NSLog(@"%@", [player description]);
    NSLog(@"%@", [sender description]);
}

Upvotes: 1

Views: 768

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726849

You can forward the sender of your showSquadSelector: method to the segue, like this:

[self performSegueWithIdentifier:@"SeguePopupSquad" sender:sender];

The sender of the segue would be the button that triggered the segue, so the code triggered from the segue would know what button has triggered it: your prepareForSegue: would have the correct UIButton. You can now add it to the returned dictionary at a predetermined key (say, @"senderButton") and examine it upon the return from the segue.

Upvotes: 1

Related Questions