0

基本上如何执行以下操作,但对于 64 个标签:

int lblInt = arc4random() % 64
if (lblInt == 0) {
    lbl.text = @"Letter";
}
4

1 回答 1

2

为什么不使用数组来代替?

NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i < 64; i++) {
    UILabel *lbl = [[UILabel alloc] initWithFrame:someFrame];
    [arr addObject:lbl];
    [someSuperview addSubview:lbl];
    [lbl release];
}

然后,您可以使用以下代码使每个标签显示一个随机字母:

for (UILabel *lbl in arr) {
    int r = arc4random() % 64;
    if (r < 26) {
        lbl.text = [NSString stringWithFormat:@"%c", 'a' + r];
    } else if (r < 52) {
        lbl.text = [NSString stringWithFormat:@"%c", 'A' + r - 26];
    } else if (r < 62) {
        lbl.text = [NSString stringWithFormat:@"%c", '0' + r - 52];
    } else {
        // whatever the last two characters should be
    }
}
于 2013-01-26T10:16:17.577 回答