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:
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?
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?
Sure, you can do that.
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];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With