簡體   English   中英

UIButton-程序化創建問題

[英]UIButton - Programmatic creation issue

我正在以編程方式創建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時,同樣的工作原理。

幫幫我。 提前致謝..!

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

我的朋友試試這個。

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

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

編碼愉快!!!

您正在此處創建按鈕:

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

我在這里指出了一些問題:

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

對於自定義按鈕,請使用以下代碼...

刪除此行

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM