太多锅炉板代码切换声明 - iOS

时间:2014-05-18 14:48:49

标签: ios cocoa-touch switch-statement uilabel boilerplate

我有一个switch语句,它会适当地将文本添加到11个UILabel。但是,它现在变得有点长,而且相同代码的副本太多了。我该如何进一步改进它?我可以制作一个标签数组,然后通过for循环访问它们吗?

这是代码切换语句:

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;
        }

谢谢你的时间,Dan。

3 个答案:

答案 0 :(得分:4)

另一种选择是使用switch语句的“fall through”属性

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;
}

答案 1 :(得分:2)

将你的UILabel放入NSArray中。然后你可以迭代数组。在Interface Builder中,您可以将一大堆元素添加到IBOutletCollection,这很方便(只需将多个内容拖放到一起,例如创建IBOutlet,它就会提供IBOutletCollection })。

请注意IBOutletCollection在订单上不做任何承诺。因此,您可能需要在IB中添加tag以跟踪它们。我经常在tag中按viewDidLoad对我的收藏品进行排序。

如果以编程方式构建此系统,则可以将它们放在数组中以开始。

有时跟踪tag仍然太痛苦,所以我有点作弊。我只是使用subviews来查看所有视图并找到我的标签。然后我按y-origin对它们进行排序并将它们粘贴在一个数组中。那当然有点脆弱,需要很多关于你的视图的特殊知识,但视图控制器应该具备这些知识。我偶尔会使用这种技术来创建一个混合IB /编程接口,它允许我在IB中放置所有内容,然后在代码中连接所有内容(操作,字体选择等)。当有很多几乎相同的UI元素时,我特别这样做。

答案 2 :(得分:2)

使用两个数组:1。用于所有标签,2。用于计数 你已经有了2.部分:

首先,在你的班级中有一个NSArray *_labels
然后在viewDidLoad中_labels = @[label1, label2, label3];

然后换班次

for(int i = 0; i < number_sorted.count; i++) {
    ((UILabel*)_labels[i]).text = [NSString stringWithFormat:@"%ld", [number_sorted[i] integerValue]];
}