简体   繁体   中英

Pass info to viewcontroller on storyboard

I'm new using storyboards and I'm facing this situation: A uinavigationcontroller contains a view controller (root controller) which contains ten buttons linked each one of then through storyboard to the same view controller.

The behavior of the second view controller depends on the button tapped in the first view controller, but how can identify which button is tapped (like tag value) and pass this info to second view controller?

Thank you.

To add on Daniel's answer:

First, add a public property onto your secondVC that is accessible from your first VC:

@interface SecondViewController : UIViewController 

@property (nonatomic) int buttonTagClicked;

@end

You need to set tags up on your UIButtons. This is either done in storyboard or programmatically in your code. I would create a generic IBAction that each button is linked to. You can extract the tag off the button through the sender parameter later.

- (IBAction)buttonClicked:(id)sender
{
  [self performSegueWithIdentifier:@"pushSegue" sender:sender];
}

That is linked up to

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"pushSegue"]) {
    SecondViewController *destinationVC = (SecondViewController *)[segue destinationViewController];
    UIButton *selectedButton = (UIButton *)sender;
    destinationVC.buttonTagClicked = selectedButton.tag;
  }
}

You can set a segueIdentifier for each connection. Then in your ViewController you could trigger an action based on the identifier you set.

eg:

If you select your connection in storyboard you can name it:

在此处输入图片说明

And in your ViewController you can trigger an action based on the identifier like this:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"segue1"]) {
        UIViewController *destinationVC = [segue destinationViewController];
        destinationVC.property = ...;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM