gpt4 book ai didi

ios - 为什么 UIButton 不出现在屏幕上?

转载 作者:行者123 更新时间:2023-11-29 00:51:22 25 4
gpt4 key购买 nike

我创建了 UIButton 但它没有出现在屏幕上。

在 .h 文件中:

@property (weak, nonatomic) IBOutlet UIButton * button;
@property (weak, nonatomic) IBOutlet UIImage * Image1;

在 .m 文件中:

_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button addTarget:self action:@selector(_button1:) forControlEvents:UIControlEventTouchUpInside];
[_button setTitle:@"Show View" forState:UIControlStateNormal];
_button.frame = CGRectMake(0, 0, 437, 765);

_Image1 = [UIImage imageNamed:@"1.png"];
[_button setBackgroundImage:_Image1 forState:UIControlStateNormal];
[self.view addSubview: _button];

为什么这个按钮不起作用?

但是这个按钮可以工作。

UIButton *button1;
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self action:@selector(swipeleft1:) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:@"Show View" forState:UIControlStateNormal];
button1.frame = CGRectMake(0, 0, 437, 765);

UIImage *buttonImage1 = [UIImage imageNamed:@"1.png"];
[button1 setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
[self.view addSubview:button1];

最佳答案

以这种方式定义您的属性:

@property (strong, nonatomic) UIButton * button;
@property (strong, nonatomic) UIImage * Image1;

Strong 意味着你的按钮和图像将被保留,在你有机会将它们添加到屏幕之前它们被释放。

IBOutlets 可能很弱,因为它们已经被 View 保留,但是您是以编程方式创建按钮,而不是使用界面构建器。

关于ios - 为什么 UIButton 不出现在屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38118713/

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