的對象的性質我有一個:無法更改的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中創建所有按鈕並創建連接。
我張貼的圖片,這樣就可以得到一個想法......
反正是獲得點擊一個按鈕時的執行方法是:
-(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++;
}
}
我以前添加的按鈕根本不會改變。阿爾法也不會改變。當我運行最後一個方法時,我不知道我在更改哪個按鈕。
非常感謝!那讓我很難過。 –
哦,我忘了提及你必須使用'NSMutableArray'實例才能夠添加對象。然後你可以像下面這樣初始化'buttonsArray':'buttonsArray = [[NSMutableArray alloc] init];' – Joost