Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have several buttons all segue to the same view controller?

I am new in iOS and storyboard and I need some advice on the possible ways of designing the architecture of the new app. I will have some data saved in the database and I will have a UIViewController page with just some buttons that have some names of some categories. When let's say 'All' is clicked or 'Business' is clicked, it will fetch data show according to that category to a UITableViewController.

Now I am confused on one part:

  1. In story board is it allowed to create multiple segues from different buttons to the same ViewController Class? So if that is not allowed, do I have to create 6 different UITableViewController classes to make segues for 6 categories?

  2. And if it is allowed to make segues from multiple buttons to a single UIViewController, how do I send a parameter in storyboard to make it realize that I clicked a specific button to go to the UIViewController.

Should I make a custom constructor in the UITableViewController which takes a parameter and I will initial that constructor from the other button click methods? So that it displays results in terms of category? Or is there a way storyboard does this more easily?

like image 343
Shahnawaz Avatar asked Sep 04 '12 06:09

Shahnawaz


1 Answers

  1. Sure, you can do that.

  2. After creating all your buttons, control-drag from each button to the next view controller and create (for example) a "push" segue. Next, click on the segue graphic to select the segue. Switch to the Attributes inspector and give segue an identifier, like "All" or "Business". You can look at the segue's identifier in your -prepareForSegue: method to figure out which segue caused the transition, and hence which category to display.

I don't recommend that approach, though. There's no need to create half a dozen segues that all do exactly the same thing. I'd use an action that triggers a single segue instead:

  • Set the tag property of each button to a unique value so that you can differentiate between the buttons.

  • Add an action method to your first view controller (the one that manages the buttons) named something like -(IBAction)switchToCategoryFromButton:(id)sender.

  • Connect all the buttons to that single action.

  • Implement the action such that it looks at sender.tag to figure out which button was pressed, uses that to determine which category to load, and then triggers the one segue to the next view controller programmatically.

Having a single segue triggered by an action seems a lot neater than many segues. Code to do that might look like this:

- (IBAction)switchToCategory:(UIControl*)sender
{
    // map the tapped button to a category
    switch (sender.tag) {
        case kAllButton: {
            self.category = kAllCategories;
            break;
        }
        case kBusinessButton: {
            self.category = kBusinessCategory;
            break;
        }
        default: {
            self.category = kAllCategories;
            break;
        }
    }

    // start the segue
    [self performSegueWithIdentifier:kCategorySegue sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // tell the destination view controller what category to display
    if ([segue.identifier isEqualToString:kCategorySegue]) {
        [(NextViewController*)segue.destinationViewController setCategory:self.category];
    }
}
like image 190
Caleb Avatar answered Nov 12 '22 07:11

Caleb