这真是太简单了!我错过了一些东西,我试图解决它已经筋疲力尽了。希望有人能提供帮助。
CharacterView.m中的Button可以正常工作,但是嵌入在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
答案 0 :(得分:17)
最后!!!!!!
我必须使用initWithFrame初始化所有视图并传入有效的帧rects。 Init应与控制器和initWithFrame一起使用,传递UIViews !!!!
characterView = [ [ CharacterView alloc ] initWithFrame:CGRectMake(0, 0, 480, 320)];
then
characterMale = [ [ CharacterMale alloc ] initWithFrame:frame];
答案 1 :(得分:17)
另一件需要考虑的事情是UIButton
个已添加UIImageView
的子视图根本不起作用。您需要创建一个UIView
,您可以将图像视图和按钮添加到。
这可能是因为图像视图默认关闭了交互,但我没有检查过这个。
答案 2 :(得分:2)
新视图是否接受用户互动?换句话说,是否在视图“Characters”上启用了userInteractionEnabled?
答案 3 :(得分:1)
在使用CGRectZero框架初始化UIView期间尝试添加按钮时,我遇到了类似的问题:
@implementation SomeView
...
- (id)initWithTitleImage:(UIImage *) newTitleImage {
// BROKEN: frame with CGRectZero.
self = [super initWithFrame:CGRectZero];
if (self) {
UIButton *someButton = ...;
[self addSubview someButton];
}
}
一旦我将框架更改为正确的矩形,按钮就会起作用:
@implementation SomeView
...
- (id)initWithTitleImage:(UIImage *) newTitleImage {
// WORKING: frame with proper CGRect.
self = [super initWithFrame:CGRectMake(0, 0, 480, 320)];
if (self) {
UIButton *someButton = ...;
[self addSubview someButton];
}
}
答案 4 :(得分:1)
我很幸运使用UIControlEventTouchDown
代替热门UIControlEventTouchUpInside
在drawRect中创建的UIButton的选择器。
答案 5 :(得分:1)
对我来说问题是因为,按钮框架的原点比父框架大。