UIButton - 程序化创建问题

iOS*_*iOS 0 iphone objective-c uibutton ios ios5

我正在以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");
}
Run Code Online (Sandbox Code Playgroud)

上面的代码没有输出.该按钮未创建.

虽然我创造时同样有效UITextField.

帮帮我.提前致谢..!

Nar*_*ana 5

你需要在init语句后设置按钮的框架,你需要以不同的方式设置按钮的标题

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];
Run Code Online (Sandbox Code Playgroud)