我是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/