gpt4 book ai didi

iphone - 无法以编程方式使 UIButton 工作 - iPhone

转载 作者:行者123 更新时间:2023-12-03 18:54:22 24 4
gpt4 key购买 nike

这是我正在使用的代码:

-(void)viewDidLoad
{
NSString *checkerPath = [[NSBundle mainBundle] pathForResource:@"black-yellow-checker" ofType:@"png"];
UIImage *checkerImage = [[UIImage alloc] initWithContentsOfFile:checkerPath];
checkerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 230.0f, 320.0f, 280.0f)];
[checkerView setImage:checkerImage];

UIButton *backButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
backButton.frame = CGRectMake(45.0f, 175.0f, 230.0f, 50.0f);
[backButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yellowButton" ofType:@"png"]] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(goBackHome:) forControlEvents:UIControlEventTouchDown];
[backButton setTitle:@"Back" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont fontWithName:@"Marker Felt" size:30];
backButton.titleLabel.textColor = [UIColor blackColor];
[checkerView addSubview:backButton];
}

- (void)goBackHome:(id)sender
{
NSLog(@"go back pressed");
}

不知道为什么这不起作用。按钮出现了,但是当我按下它时什么也没有发生。当我触摸它时,它甚至不会变成“缩进”图像。没有什么。不能使用IB,必须以编程方式。有什么想法吗?

最佳答案

UIImageView has userInteractionEnabled set to NO by default 。这可以防止任何 subview 被触摸。

您可以通过以下方式启用 ImageView 的用户交互

checkerView.userInteractionEnabled = YES;

或者创建一个新的 UIView 以包含 checkerViewbackButton

UIView* newView = ...
...
[newView addSubview:checkerView];
[newView addSubview:backButton];

关于iphone - 无法以编程方式使 UIButton 工作 - iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3859102/

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