0

Okey guys, here's the deal...

I'm trying to connect two views with one segue and writing in Label a tag from clicked button. I hope you know what I mean...

Here's the example: Title view appears and you have to choose the difficulty 1-3 (tag values of buttons) and after clicking your're moved to game view but you'd like to use tag values from title view buttons in game logic.

Here's the sample screen: enter image description here

cojoj
  • 6,405
  • 4
  • 30
  • 52

2 Answers2

3

This is what I've found and this perfectly suits!

// When any of my buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
    [self performSegueWithIdentifier:@"Segue" sender:sender];
}

// This will get called before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"Segue"]) {

        // Get destination view
        SecondView *vc = [segue destinationViewController];

        // Get button tag number (or do whatever you need to do here, based on your object
        NSInteger tagIndex = [(UIButton *)sender tag];

        // Pass the information to your destination view
        [vc setSelectedButton:tagIndex];
    }
}

And here is the link to whole topic.

Community
  • 1
  • 1
cojoj
  • 6,405
  • 4
  • 30
  • 52
2

Set your buttons to trigger the segue that presents the second view controller.

In your first view controller, implement - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender and use the sender to retrieve the tag.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195