編程創建了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");
}
什麼也沒輸出,Button沒創建成功。
但是同樣方法創建UITextField就成功了。
謝謝,請指點
在初始化語句之後要給Button設置框架,用不同方式命名Button。
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Button" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];