2013-04-04 79 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