11

我敢肯定,这该死的简单!我错过了一些东西,并且因为试图修复它而筋疲力尽。希望有人可以提供帮助。

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
4

6 回答 6

17

最后!!!!!!

我必须使用 initWithFrame 初始化所有视图并传入有效的框架矩形。Init 应该与控制器一起使用,并且 initWithFrame 为 UIViews 传递 rects!!!!

characterView = [ [ CharacterView alloc ] initWithFrame:CGRectMake(0, 0, 480, 320)]; 
then 
characterMale = [ [ CharacterMale alloc ] initWithFrame:frame];
于 2009-05-04T08:28:00.350 回答
17

要考虑的另一件事是UIButton添加的子视图UIImageView根本不起作用。您需要创建一个UIView添加图像视图和按钮的对象。

这可能是因为默认情况下图像视图上的交互是关闭的,但我没有检查这一点。

于 2010-05-24T11:20:45.360 回答
2

新视图是否接受用户交互?换句话说,是否在“Characters”视图上启用了 userInteractionEnabled?

于 2009-05-04T08:30:39.440 回答
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];
  }
}
于 2010-06-13T15:04:57.473 回答
1

通过使用UIControlEventTouchDown而不是流行的UIControlEventTouchUpInside.

于 2011-01-31T20:30:46.473 回答
1

对我来说,这个问题是因为按钮框架的原点比父框架大。

于 2013-07-26T20:50:58.560 回答