【发布时间】:2012-12-05 06:32:35
【问题描述】:
我正在以编程方式创建UIButton,如下所示。
- (void)viewDidLoad
{
[paymentsHomeView setFrame:CGRectMake(0, 0, 320, 520)]; // paymentsHomeView is added and referenced in IB
UIButton *paymentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 45, 300, 41)];
paymentButton.titleLabel.text = @"Button";
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[paymentButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
[super viewDidLoad];
}
-(void) buttonClicked {
NSLog(@"buttonClicked");
}
上面的代码没有输出。未创建按钮。
而当我创建 UITextField 时同样有效。
帮帮我。提前谢谢..!
【问题讨论】:
-
评论这个并运行它会起作用 paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];并确保将您的 paymentHomeView 添加到 self.view
-
另外你为什么在你的“
viewDidLoad”方法中在一行中引用“paymentsHomeView”,然后在另一行中引用“self. paymentsHomeView”? -
始终将
[super viewDidLoad];作为方法的第一行。除非在 dealloc 中,否则 super 调用应该是第一个调用。
标签: iphone objective-c ios ios5 uibutton