【问题标题】:UIButton - Programmatic creation issueUIButton - 程序化创建问题
【发布时间】: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


【解决方案1】:

您需要在 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];

【讨论】:

  • 谢谢你..!像魅力一样工作..!
【解决方案2】:

我的朋友试试这个..

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

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

编码愉快!!!!

【讨论】:

    【解决方案3】:

    您正在这里创建按钮:

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

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

        删除这一行

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-24
          • 1970-01-01
          相关资源
          最近更新 更多