0

我是新手objective-c.
谁能告诉我为什么在我运行代码时按钮没有出现。

这是我的代码:

    - (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

1 回答 1

4

在前面添加 [self.view addSubview:button1];initWithNibNamereturn 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???

于 2013-04-04T12:57:02.667 回答