0

我正在以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.

帮帮我。提前致谢..!

4

5 回答 5

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];
于 2012-12-05T06:36:54.403 回答
1

试试这个我的朋友..

UIButton *paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[paymentButton addTarget:self 
       action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[paymentButton setTitle:@"Show View" forState:UIControlStateNormal];
 paymentButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.paymentsHomeView addSubview:paymentButton];

让我知道它是否有效...!!!

快乐编码!!!!

于 2012-12-05T06:35:27.833 回答
1

您正在此处创建按钮:

UIButton *paymentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 45, 300, 41)];

再次在这一行上创建另一个按钮,以便覆盖前一个按钮,因此您需要再次设置框架:

paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

我建议这样做:

 paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[paymentButton setFrame:CGRectMake(10, 45, 300, 41)];

    paymentButton.titleLabel.text = @"Button";
    [paymentButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.paymentsHomeView addSubview:paymentButton];
于 2012-12-05T06:36:23.970 回答
1

我在这里指出了一些问题:

[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";
// You just allocated a paymentButton above. Without ARC it is leaked here:
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Now you have assigned an auto released button to paymentButton. It does not have a frame or text.
[paymentButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];

试试这个:

UIButton *paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
paymentButton.frame = CGRectMake(10, 45, 300, 41);
paymentButton.titleLabel.text = @"Button";
[paymentButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
于 2012-12-05T06:42:45.317 回答
0

对于自定义按钮,请使用以下代码...

删除此行

paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

并添加显示黑色背景按钮的这一行

paymentButton.titleLabel.textColor = [UIColor whiteColor];// set text color which you want
[paymentButton setBackgroundColor:[UIColor blackColor]];// set back color which you want

对于 RoundRect 按钮,使用下面的代码

    UIButton *paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    paymentButton.frame = CGRectMake(10, 45, 300, 41);
    paymentButton.titleLabel.text = @"Button";
    [paymentButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.paymentsHomeView addSubview:paymentButton];
    [self.paymentsHomeView bringSubviewToFront:paymentButton];
于 2012-12-05T06:39:04.217 回答