Iphone UIButton不能在嵌套的UIViews中工作

Cha*_*son 11 iphone uibutton uiviewcontroller uiview

这真是太简单了!我错过了一些东西,我试图解决它已经筋疲力尽了.希望有人可以提供帮助.

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
Run Code Online (Sandbox Code Playgroud)

Cha*_*son 17

最后!!!!!!

我必须使用initWithFrame初始化所有视图并传入有效的帧rects.Init应该与控制器和initWithFrame一起使用,传递UIViews!

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

  • @Roger因为其他人可能会遇到同样的问题.你不只是在解决问题时删除它... (11认同)

Kay*_*Ess 17

另一件需要考虑的事情是,UIButton添加了a的子视图UIImageView根本不起作用.您需要创建一个UIView既添加图像视图又按钮的按钮.

这可能是因为图像视图默认关闭了交互,但我没有检查过这个.

  • 凯,谢谢你的建议.我将UIImageView suerInteractionEnabled设置为yes并且它有效. (2认同)