【发布时间】:2010-10-23 13:19:37
【问题描述】:
我敢肯定,这该死的简单!我错过了一些东西,并且因为试图修复它而筋疲力尽。希望有人可以提供帮助。
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
【问题讨论】:
-
你的按钮停止工作是什么意思?它们是否已启用但不调用选择器或根本无法单击它们?另外,您如何将它们连接到选择器?如果您使用的是 IB,请检查插座和连接
-
我根本无法点击按钮,没有任何反应。如果我采用 View 类,则该按钮位于其父 View 按钮之外。我没有使用 IB。我使用以下方法连接按钮: [button addTarget:self action:@selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];当它没有嵌套到另一个视图中时,它再次工作正常。
-
我发布了所有 3 个课程。有人可以帮忙吗?
标签: iphone uiview uiviewcontroller uibutton