2012-11-28 120 views
0

我一直在研究一個應用程序,我在IB中手動創建了一打按鈕。我現在想重新開始,並且在代碼中做所有事情,因爲我需要動態的按鈕數量。我還想做其他事情像手動控制,他們出現等等等等在數組中創建UIButton並將它們添加到MainViewController中

MainViewController.h

@property (nonatomic, retain) IBOutlet UIButton *startButton; 
@property (nonatomic, retain) IBOutlet UIButton *stopButton; 
@property (nonatomic, retain) IBOutletCollection(UIButton) NSMutableArray *tiles; 
- (IBAction) tilePressed:(id)sender; 
- (void) initTiles; 

MainViewController.m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self initTiles]; 
} 

- (void)viewDidUnload 
{ 
    [self setStartButton:nil]; 
    [self setStopButton:nil]; 
    [self setTiles:nil]; 
    [super viewDidUnload]; 
} 

-(void)initTiles 
{ 
//tiles = [[NSMutableArray alloc] initWithCapacity:5]; 

    for (int count = 0; count < 5; count++) 
    { //I used tiles.count here, but that fails 
    UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(count*10+10, 10, 10, 10)]; 
    [b setTitle:[NSString stringWithFormat:@"%i",count+1] forState:UIControlStateNormal]; 
    [b setTag:count]; 
    [b setHidden:NO]; 
    [b addTarget:self action:@selector(tilePressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:b]; 
    } 
} 

-(IBAction)tilePressed:(id)sender { 
    NSLog(@"button pressed: %i", [sender tag]); 
} 

我一直試圖做的是隻需在屏幕上添加5個按鈕。理想情況下,我想創建按鈕,將它們放在一個數組(NSMutableArray)中,以便稍後操作它們,然後在MainViewController上投擲tiles。但我甚至沒有沒有瓷磚對象的控制器的按鈕。

編輯:我想我會補充一點,它們屬於一個數組並不重要,重要的是我可以稍後與它們進行交互,也就是說,會出現動畫和文本變化等等。最初的問題然而,就是他們沒有顯示在控制器上

回答

3

你的代碼實際上完美的作品,但你真的不能說,因爲:

  • 按鈕是如此之小,他們幾乎是不可能的使用
  • 按鈕太小,文本標題不會顯示
  • 背景顏色爲零,所以它默認爲清除,這使得整個按鈕在這種情況下不可見。

爲了明顯地,只要按鈕更大,改變背景顏色:

-(void)initTiles 
{ 
    //tiles = [[NSMutableArray alloc] initWithCapacity:5]; 

    for (int count = 0; count < 5; count++) 
    { //I used tiles.count here, but that fails 
     UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(count*50+10, 50, 50, 50)]; 
     [b setTitle:[NSString stringWithFormat:@"%i",count+1] forState:UIControlStateNormal]; 
     [b setTag:count]; 
     [b setHidden:NO]; 
     [b addTarget:self action:@selector(tilePressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [b setBackgroundColor:[UIColor purpleColor]]; 
     [self.view addSubview:b]; 
    } 
} 

現在,只要將它們添加到陣列:

-(void)initTiles 
{ 
    self.tiles = [[NSMutableArray alloc] initWithCapacity:5]; 

    for (int count = 0; count < 5; count++) 
    { //I used tiles.count here, but that fails 
     UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(count*50+10, 50, 50, 50)]; 
     [b setTitle:[NSString stringWithFormat:@"%i",count+1] forState:UIControlStateNormal]; 
     [b setTag:count]; 
     [b setHidden:NO]; 
     [b addTarget:self action:@selector(tilePressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [b setBackgroundColor:[UIColor purpleColor]]; 
     [self.tiles addObject:b]; 
     [self.view addSubview:b]; 
    } 
} 

然後,您可以通過使用數組訪問按鈕!

+0

我不覺得自己像一個昏暗的機智大聲笑。謝謝你:) – Madivad

+0

不用擔心,直到你知道它並不明顯! :-) – lnafziger

相關問題