简体   繁体   English

iPhone UIButton不起作用

[英]Iphone UIButton not working

This is so damn simple im sure! 这太简单了,我确定! Im missing something and im exhausted from trying to fix it. 我错过了一些东西,尝试修复它时筋疲力尽。 hopefully someone can help. 希望有人可以提供帮助。

The Button in CharacterView.m works but the button nested down in CharacterMale.m does not. CharacterView.m中的按钮可以使用,但嵌套在CharacterMale.m中的按钮则不能。 I'm not using IB everything is done progmatically. 我不是在使用IB,而是通过编程来完成所有工作。

CharacterView.m is being used as a container CharacterView.m被用作容器

/////////////////////////////////////////////////////////////////////////////////
 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 {
    [characterView release];
    [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 {
    [characterMale release];
    [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

FINALLY!!!!!! 最后!!!!!!

I had to init all the views with initWithFrame and pass in valid frame rects. 我必须使用initWithFrame初始化所有视图,并传递有效的帧矩形。 Init should be used with controllers and initWithFrame passing rects for UIViews!!!! 初始化应该与UIViews的控制器和initWithFrame传递矩形一起使用!!!!

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

我也是这个新手,但是您是否尝试过:UIControlEventValueChanged而不是UIControlEventTouchUpInside?

How big is the charMale.png image? charMale.png图像有多大? What is the z-Order for the image and button in CharacterMale? CharacterMale中图像和按钮的z顺序是什么? The image may be sitting on top of the button that you are creating preventing it from being touched. 图像可能位于您正在创建的按钮上方,从而防止触摸它。

Hopefully this is the problem... I've not seen anything else yet... 希望这是问题所在...我还没有看到其他任何东西...

Please check your button frame. 请检查您的按钮框。 You have set (0,0,200,100); 您已设置(0,0,200,100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM