簡體   English   中英

在循環中以編程方式創建 UILabel 后如何更改它的文本

[英]How to alter the text of an UILabel after having created it programmatically in a loop

我想以編程方式創建 10 個按鈕。 每個按鈕都有一個帶有兩個標簽(一個字符串和一個整數)的子視圖。

在循環創建這 10 個按鈕后,我想訪問這兩個標簽。 我嘗試使用標簽 0 對按鈕的標簽進行 NSLog 記錄,但它沒有用。

所有這些都感覺有點笨拙,所以如果我對此無處可去,請糾正我:

for (int i = 0; i < 9; i++) {

    UIButton* btn = [[[UIButton alloc] initWithFrame:CGRectMake(55, i*(indexHeight+indexSpacing), indexWidth, indexHeight)] autorelease]; 
    btn.tag = i;
    [btn setBackgroundImage:nil forState:UIControlStateNormal];
    [btn    addTarget:self
            action:@selector(buttonTapped:)
  forControlEvents:UIControlEventTouchUpInside];

 // LABELS

    UILabel *btnTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, indexWidth, indexHeight)] autorelease];
     btnTitle.text = @"Empty";

    UILabel *pageTitle = [[[UILabel alloc] initWithFrame:CGRectMake(190, 0, 30, indexHeight)] autorelease];
     pageTitle.text = @"x";

    [indexView addSubview:btn];
    [btn addSubview:btnTitle];
    [btn addSubview:pageTitle];
}

這是我嘗試過的,也是我的問題所在:

NSLog (@"Accessing label 1 in button with tag 0 in indexView: '%@'", [[indexView viewWithTag:0] btnTitle.text]);
NSLog (@"Accessing label 2 in button with tag 0 in indexView: '%@'", [[indexView viewWithTag:0] pageTitle.text]);

UILabel 以編程方式創建 - 再次找到它? ” 是我能找到的最接近的問題,但它並沒有真正回答如何在 btn 的子視圖中訪問標簽的問題。

您也可以嘗試為每個標簽分配一個標簽,例如第一個標簽為 100,第二個標簽為 101,然后您的代碼將如下所示用於檢索標簽

NSLog (@"Accessing label 1 in button with tag 0 in indexView: '%@'", [[indexView viewWithTag:0] viewWithTag:100].text);
NSLog (@"Accessing label 2 in button with tag 0 in indexView: '%@'", [[indexView viewWithTag:0] viewWithTag:101].text);

如果設置了 indexHeight、indexSpacing、indexWidth,則以下內容肯定有效。

for (int i = 0; i < 9; i++) {

    UIButton* btn = [[[UIButton alloc] initWithFrame:CGRectMake(55, i*(indexHeight+indexSpacing), indexWidth, indexHeight)] autorelease]; 
    btn.tag = i;
    [btn setBackgroundImage:nil forState:UIControlStateNormal];
    [btn    addTarget:self
            action:@selector(buttonTapped:)
  forControlEvents:UIControlEventTouchUpInside];

 // LABELS

    UILabel *btnTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, indexWidth, indexHeight)] autorelease];
     btnTitle.text = @"Empty";

    btnTitle.tag = i+100;
    UILabel *pageTitle = [[[UILabel alloc] initWithFrame:CGRectMake(190, 0, 30, indexHeight)] autorelease];
     pageTitle.text = @"x";
     pageTitle.tag = i+101;

    [indexView addSubview:btn];
    [btn addSubview:btnTitle];
    [btn addSubview:pageTitle];

}

for (int i = 0; i < 9; i++) {
   UILabel *btnTitle = (UILabel *)[[indexView viewWithTag:i] viewWithTag:100+i];
   UILabel *pageTitle = (UILabel *)[[indexView viewWithTag:i] viewWithTag:101+i];
   NSLog (@"btnTitle.text: '%@'", btnTitle.text);
   NSLog (@"pageTitle.text: '%@'", pageTitle.text);

}

1

在您的 header 文件中添加此 ivar:

NSMutableArray *buttonArray;

2

在 your.m 文件的 init 中初始化此數組:

buttonArray = [NSMutableArray array];
[buttonArray retain];

3

改變你的 for 循環如下:

for (int i = 0; i < 9; i++) 
    {

        UIButton* btn = [[[UIButton alloc] initWithFrame:CGRectMake(55, i*(indexHeight+indexSpacing), indexWidth, indexHeight)] autorelease]; 
        btn.tag = i;
        [btn setBackgroundImage:nil forState:UIControlStateNormal];
        [btn    addTarget:self
                   action:@selector(buttonTapped:)
         forControlEvents:UIControlEventTouchUpInside];

        // LABELS

        UILabel *btnTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, indexWidth, indexHeight)] autorelease];
        btnTitle.text = @"Empty";

        UILabel *pageTitle = [[[UILabel alloc] initWithFrame:CGRectMake(190, 0, 30, indexHeight)] autorelease];
        pageTitle.text = @"x";

        [indexView addSubview:btn];
        [btn addSubview:btnTitle];
        [btnTitle release]; //avoiding leaks
        [btn addSubview:pageTitle];
        [pageTitle release]; //avoiding leaks






        //The changes are here.....

        [buttonArray addObject:btn];
        [btn release];
    }

4

要訪問數組中的按鈕(例如按鈕 5):

UIButton *thisButton = (UIButton *)[buttonArray objectAtIndex:5];

5

用這個按鈕做任何你想做的事:)

PS:您在 for 循環中分配那些 btn 對象(並且似乎它們不是 ivars)。 一旦您在 for 循環之外,他們將失去 scope ,因此您無論如何都無法訪問它們。 使用 NSMutableArray 的上述方法也解決了這個問題,因為現在您已經創建了 btn 並將它們添加到一個 ivar 數組中,因此您不會在 for 循環之外丟失 scope。 此外,如果您想要自定義子視圖,您可能應該繼承 UIButton。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM