我是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];
}

最佳答案

[self.view addSubview:button1];之前的initWithNibName中添加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

关于ios - UIButton不出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15811714/

10-12 07:25