gpt4 book ai didi

Iphone UIButton 无法在嵌套 UIView 中工作

转载 作者:行者123 更新时间:2023-12-03 18:27:49 25 4
gpt4 key购买 nike

我确信这太简单了!我错过了一些东西,并且由于试图修复它而筋疲力尽。希望有人能帮忙。

CharacterView.m 中的按钮可以工作,但嵌入到CharacterMale.m 中的按钮却不能。我没有使用 IB,一切都是通过编程完成的。什么会导致一个按钮工作而其他按钮不工作?

/////////////////////////////////////////////////////////////////////////////////
CharacterController.m
/////////////////////////////////////////////////////////////////////////////////
#import "CharacterController.h"
#import "CharacterView.h"

@implementation CharacterController

- (id)init {
NSLog(@"CharacterController init");
self = [ super init ];
if (self != nil) {
}
return self;
}

- (void)loadView {
[ super loadView ];
characterView = [ [ CharacterView alloc ] init];
self.view = characterView;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}


- (void)dealloc {
[super dealloc];
}

@end

/////////////////////////////////////////////////////////////////////////////////
CharacterView.m
/////////////////////////////////////////////////////////////////////////////////

#import "CharacterView.h"
#import "CharacterMale.h"

@implementation CharacterView

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
characterMale = [ [ CharacterMale alloc ] init];
[self addSubview: characterMale];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 200, 200, 100);
[button setImage:[UIImage imageNamed:@"btnCharSelect.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];
[ self addSubview: button ];
}
return self;
}

- (void)drawRect:(CGRect)rect {
}

-(void)ApplyImage:(id)sender
{
NSLog(@"CharacterView button works");
}

- (void)dealloc {
[super dealloc];
}

@end

/////////////////////////////////////////////////////////////////////////////////
CharacterMale.m
/////////////////////////////////////////////////////////////////////////////////

#import "CharacterMale.h"
#import "CharacterController.h"

@implementation CharacterMale

- (id)init {
self = [ super init];
if (self != nil) {
UIImage *image = [UIImage imageNamed:@"charMale.png"];
imageView = [[ UIImageView alloc] initWithImage:image];
[image release];
[ self addSubview: imageView ];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200, 100);
[button setImage:[UIImage imageNamed:@"btnCharSelect.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];
[ self addSubview: button ];

}
return self;
}

-(void)ApplyImage:(id)sender
{
NSLog(@"CharacterMal button works");
}

- (void)dealloc {
[super dealloc];
}

@end

最佳答案

终于!!!!!!

我必须使用 initWithFrame 初始化所有 View 并传入有效的框架矩形。 Init 应该与 Controller 一起使用,并且 initWithFrame 传递 UIView 的矩形!!!!

characterView = [ [ CharacterView alloc ] initWithFrame:CGRectMake(0, 0, 480, 320)]; 
then
characterMale = [ [ CharacterMale alloc ] initWithFrame:frame];

关于Iphone UIButton 无法在嵌套 UIView 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/818739/

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