iqueqiorio
iqueqiorio

Reputation: 1187

Multiple buttons with same segueIdentifer

I have a tableviewController with a bunch of buttons on it, and when they are tapped I want to push to a new view controller, and I am using the prepareForSegue method and then using control drag in the story board.

In prepareForSegue I have

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showItems"]) {
}

And then a bunch of stuff that I am sending to the next view controller, so in the storyboard all of my buttons have the same identifier "showItems" which is giving me a warning of"Multiple segues have same identifer showItems" What is the best way to get rid of this warning? I still want all the buttons to do the same thing, is the a better practice for pushing the next viewController.

Thanks for the help in advance.

EDIT

- (IBAction)showItems:(id)sender{

    [self performSegueWithIdentifier:@"showItem" sender:self];
}

I have connected all the buttons to the IBAction, but then where would I pass the data to the next view controller?

In PrepareForSegue I have

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

UIButton *senderButton=(UIButton *)sender;

NSInteger meal = senderButton.tag % 10;
}

But this throws exception unrecognized selector sent to instance 0x7fcc1a496880 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DiningMealsTableViewController tag]: unrecognized selector sent to instance 0x7fcc1a496880'

Upvotes: 0

Views: 678

Answers (2)

radiovisual
radiovisual

Reputation: 6458

You only need one segue identifier, which identifies the movement from one UIViewController to another. Then inside the "calling" UIViewcontroller (the UIViewController that owns the tableview and buttons) you can trigger that Segue with a custom function that each of your buttons will call.

Like this:

- (void)viewDidLoad {

    [super viewDidLoad];

    // Create the buttons and assign them unique tags
    // and assign them a custom click handler 
    UIButton *button1 = [[UIButton alloc] initWithFrame:button1Frame];
    button1.tag = 1;
    [button1 addTarget:self action:@selector(handleButtonPress:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *button2 = [[UIButton alloc] initWithFrame:button2Frame];
    button2.tag = 2;
    [button2 addTarget:self action:@selector(handleButtonPress:) forControlEvents:UIControlEventTouchUpInside];

    // Add the buttons to the view or to your UITableViewCell
    [self.view addSubview:button1];
    [self.view addSubview:button2];
}

- (void)handleButtonPress:(id)sender {

    // get the tag you assigned to the buttons
    // to identifiy which one was pressed
    UIButton *btn = sender;
    int tag = btn.tag;
    NSLog(@"You clicked button with tag: %d", tag);

    if (tag == 1){
        NSLog(@"You clicked on Button1");
        // Do something custom here.
    } else if (tag == 2) {
        NSLog(@"You clicked on Button2");
        // Do something custom here.
    }

    // Now this is where all your buttons can call the same SegueIdentifier
    [self performSegueWithIdentifier:@"showItems" sender:sender];

}

Obviously I don't know much about the structure of your application, and my example adds the buttons to the root view, in your app you would have this same setup, only attached to the buttons you placed in your UITableViewCell but this one way you can get away with having multiple buttons trigger the same seque without having to explicitly assign a seque identifier in multiple places. It has the added benefit of being able to perform custom functions for each button before calling -performSegueWithIdentifier:sender, allowing added flexibility to your app.

Hope this helps!

Upvotes: 1

Fabio Felici
Fabio Felici

Reputation: 2906

You can create a single segue using control drag from the tableviewController to the other viewController and fire the segue in the IBAction of the button. You can create only one IBAction for all buttons and use the sender parameter to recognize the button.

Upvotes: 3

Related Questions