gpt4 book ai didi

iphone - UIButton - 编程创建问题

转载 作者:行者123 更新时间:2023-11-28 18:06:33 24 4
gpt4 key购买 nike

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

帮帮我。提前致谢...!

最佳答案

你需要在初始化语句之后为按钮设置框架,你需要以不同的方式为按钮设置标题

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

关于iphone - UIButton - 编程创建问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13717591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com