2013-04-04 121 views
0

我是objective-c.

的新手誰能告訴我爲什麼當我運行代碼時按鈕沒有出現。沒有出現UIButton

這裏是我的代碼:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    button1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]initWithFrame:CGRectMake(3, 3, 30, 30)]; 
    [button1 setTitle:@"haha" forState:UIControlStateNormal]; 
    [button1 setBackgroundColor: [UIColor blackColor]]; 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.view addSubview:button1]; 
}  

回答

4

initWithNibName添加[self.view addSubview:button1];前行return self;

其他明智

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    button1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]initWithFrame:CGRectMake(3, 3, 30, 30)]; 
    [button1 setTitle:@"haha" forState:UIControlStateNormal]; 
    [button1 setBackgroundColor: [UIColor blackColor]]; 
    [self.view addSubview:button1]; 
} 

編輯:

檢查,你是否設置了@property@synthesize,正確嗎???

+0

謝謝你的回答,但它仍然無法正常工作。 :( – lncnb91 2013-04-04 13:03:24

+0

是的,我將button1設置爲屬性 – lncnb91 2013-04-04 13:13:49

+0

「2013-04-04 22:15:52.837按鈕[3548:c07]應用程序窗口預計在應用程序啓動結束時有一個根視圖控制器。在控制檯的信息,你可以找到一些appDelegate.m – lncnb91 2013-04-04 13:21:30