How do multiple buttons go to the same controller?

I'm new to iOS and storyboard, and I need advice on possible ways to design the architecture of a new application. I will have some data stored in the database, and I will have a UIViewController page with several buttons that have some names of some categories. When you can click All or click Business, it will display the data according to this category in the UITableViewController.

Now I am confused on one side:

  • Is it allowed to create several segments from different buttons in the bulletin board into the same ViewController class? So if this is not allowed, do I need to create 6 different UITableViewController classes to make segues for 6 categories?

  • And if it is allowed to do segues from several buttons to one UIViewController, how can I send the parameter to the storyboard so that it understands that I pressed a specific button to go to the UIViewController.

Should I create a custom constructor in a UITableViewController that takes a parameter, and I initialize this constructor from other button click methods? To display results in terms of category? Or is there a way to storyboard make it easier?

+6
source share
2 answers
  • Of course you can do it.

  • After creating all your buttons, drag the control from each button to the next view controller and create (for example) a "push" segue. Then click on the segue chart to select a segment. Go to the attribute inspector and give the segue identifier, for example, "All" or "Business". You can look at the segue identifier in your -prepareForSegue: method to find out which segue caused the transition, and therefore which category to display.

I do not recommend this approach. There is no need to create half a dozen seg that all do exactly the same. I would use an action that starts only one session:

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

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

  • Connect all the buttons with this single action.

  • Implement the action so that it sender.tag at sender.tag to find out which button has been clicked, uses this to determine which category to load, and then programmatically starts one session on the next view controller.

Having one session triggered by an action seems a lot neater than many segments. The code for this 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]; } } 
+8
source

Use
self.performSegueWithIdentifier ("yourViewSegue", sender: sender) in your event to access the button button:

  @IBAction func redButtonClicked(sender: AnyObject) { self.performSegueWithIdentifier("redView", sender: sender) } 

redView is the segue identifier.

You can use the same function to handle button events, and you can distinguish button presses by assigning different tags to them.

+1
source

Source: https://habr.com/ru/post/924496/


All Articles