【问题标题】:Iphone UIButton not working in nested UIViewsIphone UIButton 在嵌套的 UIViews 中不起作用
【发布时间】: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


【解决方案1】:

终于!!!!!!

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

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

【讨论】:

  • @Roger 因为其他人可能有同样的问题。你不只是在问题解决后删除它......
【解决方案2】:

要考虑的另一件事是添加了UIImageViewUIButton 子视图根本不起作用。您需要创建一个UIView,将图像视图和按钮都添加到其中。

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

【讨论】:

  • 凯,谢谢你的建议。我将 UIImageView suerInteractionEnabled 设置为 yes 并且它起作用了。
【解决方案3】:

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

【讨论】:

  • 我以为我读过userInteractionEnabled 默认是YES。我认为在这种情况下,我会为 CharactersView 类设置 userInteractionEnabled = NO。我认为这个视图以某种方式阻止了我的 CharactersMale 类中的按钮。无论如何我都试过了,没有任何区别。
  • 我有一个包含一堆按钮的视图。我 addView 一个新视图,其中有几个自己的按钮。所有按钮都有效,包括第一层和第二层(顶层)之间的按钮。所以我嵌套按钮应该没有问题。但是如果我在 superLayer 中关闭 userInteraction,所有按钮都会停止工作。如果我只在顶层关闭 userInteraction,它只会禁用该层上的按钮。坚持下去。我敢打赌你会发现一个简单的错误。
【解决方案4】:

在初始化 UIView 时尝试添加按钮时,我遇到了类似的问题:帧为 CGRectZero:

@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];
  }
}

【讨论】:

    【解决方案5】:

    我很幸运地使用了UIControlEventTouchDown 而不是流行的UIControlEventTouchUpInside 在drawRect 中创建的UIButton 选择器。

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        • 2012-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-07
        相关资源
        最近更新 更多