Hey developers!
I built a Table View, populated it with array objects and I implemented a code when a user clicks an object inside my Table View, it loads that object’s own nib file, and I am using an if statement. (just a little note, I populated my Table View with 40 objects because that’s how much things I have and need), so I’ll show only the first two if statements I created at the beginning of my if statement code since I have way too many if statements for all 40 objects in my Table View:
if ([[glossaryArray objectAtIndex:indexPath.row] isEqual:@"Title"]) {
Title *titleLoad = [[Title alloc] initWithNibName:@"Title" bundle:nil];
[self.navigationController pushViewController:titleLoad animated:YES];
[titleLoad release];
}
else if ([[glossaryArray objectAtIndex:indexPath.row] isEqual:@"Meta Description Tag"]) {
MetaDescriptionTag *metaDescriptionTagLoad = [[MetaDescriptionTag alloc] initWithNibName:@"MetaDescriptionTag" bundle:nil];
[self.navigationController pushViewController:metaDescriptionTagLoad animated:YES];
[metaDescriptionTagLoad release];
}
...
So that’s it, I don’t want to be cocky showing all of my if statements, so yep this code works, each array objects loading it’s own separate nib file (40 nib file 1 for each array objects) and each of these nib files are like Web Views, Image Views, Text Fields and other objects. Call me crazy, but this is the only code I can think of right now, so hopefully someone can help me edit this code into a much easier code, thanks
If your values in
glossaryArraycan be mechanically transformed into the corresponding class/nib name (e.g. by removing all spaces), you could do something like this:If they can’t be mechanically transformed, you could always construct an NSDictionary mapping from one to the other, or special-case the few that are different. Or rename the classes and nibs.