UIButton - 程序化创建问题

时间:2012-12-05 06:32:35

标签: iphone objective-c ios ios5 uibutton

我正在以编程方式创建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时,同样有效。

帮帮我。在此先感谢..!

5 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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];

告诉我工作与否...... !!!

快乐编码!!!!

答案 2 :(得分: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];

答案 3 :(得分: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];

答案 4 :(得分: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 Button,请使用以下代码

    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];