我创建了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];
答案 0 :(得分:1)
以这种方式定义您的属性:
@property (strong, nonatomic) UIButton * button;
@property (strong, nonatomic) UIImage * Image1;
强意味着您的按钮和图像会在您有机会将其添加到屏幕之前被取消分配。
IBOutlets可能很弱,因为它们已被视图保留,但是您是以编程方式创建按钮,而不是使用界面构建器。