我有大約10-12個按鈕添加到我的滾動視圖中。我怎樣才能使這些成爲一個按鈕陣列,以便我可以簡化代碼?截至目前我的代碼(僅前三個按鈕顯示)如下:將一系列按鈕組合到一個數組中ios
UIButton *redButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
redButton.frame = CGRectMake(0, 0, 50, 30);
redButton.tag = 2;
[redButton setTitle:@"red" forState:UIControlStateNormal];
redButton.backgroundColor = [UIColor redColor];
[redButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[redButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:redButton];
UIButton *blueButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
blueButton.frame = CGRectMake(70, 0, 50, 30);
blueButton.tag = 3;
[blueButton setTitle:@"blue" forState:UIControlStateNormal];
blueButton.backgroundColor = [UIColor blueColor];
[blueButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[blueButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:blueButton];
UIButton *greenButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
greenButton.frame = CGRectMake(140, 0, 50, 30);
greenButton.tag = 5 ;
[greenButton setTitle:@"green" forState:UIControlStateNormal];
greenButton.backgroundColor = [UIColor greenColor];
[greenButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[greenButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:greenButton];
...
你的意思是保存按鈕(這樣你,你看這是可能的是簡化使用它們),還是配置它們(然後真的意味着如何編寫一個循環來配置我的按鈕)? – Wain 2013-05-03 16:03:35
對不起,是的,我的意思是一個循環來配置。 – DaveLass 2013-05-03 16:14:28