I have a switch statement which will add texts to 11 UILabels appropriately. However, its getting a bit long now and there is far too much copies of the same code. How can I further improve it? Could I maybe make an array of labels and then just access them via a for loop??
Here is code switch statement:
switch ([number_sorted count]) {
case 1:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
break;
case 2:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
break;
case 3:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
break;
case 4:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
break;
case 5:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
break;
case 6:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
break;
case 7:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
label_7.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[6] integerValue]];
break;
case 8:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
label_7.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[6] integerValue]];
label_8.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[7] integerValue]];
break;
case 9:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
label_7.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[6] integerValue]];
label_8.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[7] integerValue]];
label_9.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[8] integerValue]];
break;
case 10:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
label_7.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[6] integerValue]];
label_8.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[7] integerValue]];
label_9.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[8] integerValue]];
label_10.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[9] integerValue]];
break;
case 11:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
label_7.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[6] integerValue]];
label_8.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[7] integerValue]];
label_9.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[8] integerValue]];
label_10.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[9] integerValue]];
label_11.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[10] integerValue]];
break;
default:
break;
}
Thanks for your time, Dan.
Another option is to use the "fall through" property of switch statements
switch ([number_sorted count]) {
case 11:
label_11.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[10] integerValue]];
case 10:
label_10.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[9] integerValue]];
case 9:
label_9.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[8] integerValue]];
case 8:
label_8.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[7] integerValue]];
case 7:
label_7.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[6] integerValue]];
case 6:
label_6.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[5] integerValue]];
case 5:
label_5.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[4] integerValue]];
case 4:
label_4.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[3] integerValue]];
case 3:
label_3.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[2] integerValue]];
case 2:
label_2.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[1] integerValue]];
case 1:
label_1.text = [NSString stringWithFormat:@"%ld", (long)[number_sorted[0] integerValue]];
default:
break;
}
Just put your UILabel's in an NSArray. Then you can iterate over the array. In Interface Builder, you can add a bunch of elements to an IBOutletCollection
, which is convenient (just drag multiple things together, like creating an IBOutlet
, and it'll offer to make an IBOutletCollection
).
Just note that IBOutletCollection
makes no promises on order. So you may need to add a tag
in IB to keep track of them. I often sort my collection by tag
in viewDidLoad
.
If you build this system programmatically, you can just put them in the array to start with.
Sometimes keeping track of tag
is still too much of a pain, so I cheat a bit. I just use subviews
to walk all the views and find my labels. Then I sort them by y-origin and stick them in an array. That of course is a bit fragile and requires a lot of special knowledge about your view, but a view controller is expected to have that knowledge. I occasionally use this technique to create a hybrid IB/programmatic interface, which lets me lay everything out in IB, but then wire everything up (actions, font selection, etc) in code. I especially do that when there are a lot of almost identical UI elements.
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