2011-08-12 23 views
0

的對象的性質我有一個:無法更改的NSArray

@property(nonatomic, retain) NSArray * buttonsArray; 

... 
... 
@synthesize buttonsArray; 

當視圖加載我初始化爲:

buttonsArray = [[NSArray alloc] initWithObjects: 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          [UIButton buttonWithType:UIButtonTypeRoundedRect], 
          nil]; 

//這個代碼將按鈕從按鈕陣列在我看來,圖像的頂部。我將這些圖像放在名爲imagesArrayV的數組中;

int counter = 0; 


    counter=0; 
    for (UIButton *button in buttonsArray) { 

     button = [buttonsArray objectAtIndex:counter]; 
     [button setTag:counter]; // ********* 
     button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchDown]; 
     [button setTitle:@"Hello" forState:UIControlStateNormal]; 

     UIImageView *tempImage = [imagesArrayV objectAtIndex:counter]; 
     CGRect tempRect = tempImage.frame; 
     button.frame = tempRect; 

     [self.ViewMainV addSubview:button]; 
     counter++; 
    } 

這樣做的目的是爲了節省時間在xcode中創建所有按鈕並創建連接。

enter image description here

我張貼的圖片,這樣就可以得到一個想法......

反正是獲得點擊一個按鈕時的執行方法是:

-(void) test: (id) sender{ 


    UIButton*btn = (UIButton*)(sender); 


    int tagnumber = [btn tag]; 

    NSLog(@"%i",tagnumber); 

} 

那爲什麼當我按下按鈕時,當我在創建按鈕時將標記設置爲其他內容(查找:// *********)時,標記等於0。此外,當我運行這種方法:

-(void) someOtherMethod{ 
    int counter = 0; 
    for (UIButton *button in buttonsArray) { 
     button = [buttonsArray objectAtIndex:counter]; 
     button.alpha = 0; 
     button.titleLabel.text = @"I change the title"; 
     counter++; 
    } 
} 

我以前添加的按鈕根本不會改變。阿爾法也不會改變。當我運行最後一個方法時,我不知道我在更改哪個按鈕。

回答

4

就在您設置標籤的行的下方,您有行button = [UIButton buttonWithType:UIButtonTypeRoundedRect];。這顯然會覆蓋按鈕。然後該操作被添加到新創建的按鈕中,數組中的按鈕保持不變。

我個人如下將重寫代碼:

for (int counter = 0; counter < numberOfButtons; ++counter) { 
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setTag:counter]; 
    [button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchDown]; 
    [button setTitle:@"Hello" forState:UIControlStateNormal]; 

    UIImageView *tempImage = [imagesArrayV objectAtIndex:counter]; 
    CGRect tempRect = tempImage.frame; 
    button.frame = tempRect; 

    [self.ViewMainV addSubview:button]; 
    [buttonsArray addObject:button]; 
} 

這也避免了填充硬編碼的陣列,更靈活,更不容易出錯代碼。

+0

非常感謝!那讓我很難過。 –

+0

哦,我忘了提及你必須使用'NSMutableArray'實例才能夠添加對象。然後你可以像下面這樣初始化'buttonsArray':'buttonsArray = [[NSMutableArray alloc] init];' – Joost

0

嘗試使用此代碼,而不是上面的第三個部分組成:

for(int i=0;i<[buttonsArray count];i++){ 
    UIButton *button=[buttonsArray objectAtIndex:i]; 
    [button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchDown]; 
    [button setTitle:@"Hello" forState:UIControlStateNormal]; 

    UIImageView *tempImage = [imagesArrayV objectAtIndex:counter]; 
    button.frame   = tempImage.frame; 
    button.tag    =i; 
    [self.ViewMainV addSubview:button]; 
} 

的問題之一,就是你設置標籤上的按鈕,然後更換下一行的按鈕實例。

0

這看起來可疑,你做兩次:

for (UIButton *button in buttonsArray) { 

    button = [buttonsArray objectAtIndex:counter]; 

您不應該修改循環內的循環枚舉變量按鈕。你只需使用按鈕就可以了。

督察,你要麼做:

for (counter = 0; counter < buttonsArray.count; counter++) 
{ 
    UIButton *button = [buttonsArray objectAtIndex: counter]; 
    button.alpha = 0; 
    // etc... 

,或者你只是擺脫counter做:

for (UIButton * button in buttonsArray) 
{ 
    button.alpha = 0; 
    // etc...