2011-03-11 87 views
4

這是我使用的代碼:爲什麼我的UIButton不顯示?

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 50, 70)]; 
    [self.view addSubview:button]; 
    if (buttonArray == nil) { 
     buttonArray = [[NSMutableArray alloc] init]; 
    } 
    [buttonArray addObject:button]; 
    [button release]; 

的對接不顯示,但是。任何想法爲什麼?

回答

8

您正在初始化錯誤。嘗試以下操作:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
button.frame = CGRectMake(100, 200, 50, 70); 
[self.view addSubview:button]; 
if (buttonArray == nil) { 
    buttonArray = [[NSMutableArray alloc] init]; 
} 
[buttonArray addObject:button]; 
+0

我不認爲你應該釋放'按鈕',如果你這樣初始化它。 – TPoschel 2011-11-21 18:40:49

+0

@TPoschel你說得很對。更新了答案。 – 2011-11-23 13:41:21