【问题标题】:Iphone UIButton not workingiPhone UIButton 不工作
【发布时间】:2009-05-04 18:11:11
【问题描述】:

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

CharacterView.m 中的按钮有效,但 CharacterMale.m 中嵌套的按钮无效。我没有使用 IB,一切都是以编程方式完成的。

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

【问题讨论】:

  • 您是否将按钮事件设置为 NIB 文件中的 First Responder?
  • NIB 文件?我没有使用 IBuilder。对吗?
  • 你 CharacterController 正在 loadView 中泄漏 characterView
  • 你也在泄露 characterMale 和和 imageView
  • 我知道。我什至没有打扰内存管理。我试图解决这个问题!我会在代码视图中修复。

标签: iphone objective-c cocoa-touch button


【解决方案1】:

终于!!!!!!

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

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

【讨论】:

  • 我很高兴。抱歉,我无法提供更多帮助,但我很高兴你解决了它。
  • 只要我能解决下一个必然会发生的附加问题 :) LOL
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    charMale.png 图片有多大? CharacterMale 中图像和按钮的 z-Order 是什么?图像可能位于您正在创建的按钮的顶部,以防止它被触摸。

    希望这是问题所在……我还没有看到其他任何东西……

    【讨论】:

    • 我已经在 CharacterMale.m 中注释掉了图片加载,但仍然有同样的问题:(谢谢
    • 远射,但你能把头文件也贴出来吗?另外,如果您进行干净的重建,是否有任何警告?
    【解决方案4】:

    请检查您的按钮框架。 您已设置 (0,0,200,100);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 2011-04-21
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多