Iphone UIButton not working in nested UIViews

2020-04-02 09:24发布

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. I'm not using IB everything is done progmatically. What would cause one button to work and other not?

/////////////////////////////////////////////////////////////////////////////////
 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

6条回答
虎瘦雄心在
2楼-- · 2020-04-02 09:31

I had a similar issue when tried to add the button during the initialization of an UIView with a frame of CGRectZero:

@implementation SomeView
...
- (id)initWithTitleImage:(UIImage *) newTitleImage {
  // BROKEN: frame with CGRectZero.
  self = [super initWithFrame:CGRectZero];
  if (self) {
    UIButton *someButton = ...;
    [self addSubview someButton];
  }
}

Once I changed the frame to a proper rectangle, the button worked:

@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];
  }
}
查看更多
我只想做你的唯一
3楼-- · 2020-04-02 09:32

Another thing to consider is that UIButton subviews that have been added a UIImageView don't work at all. You need to create a UIView that you add both the image view and the buttons to.

This is probably because interaction is turned off by default on image views, but I've not checked this.

查看更多
神经病院院长
4楼-- · 2020-04-02 09:34

I have had luck with a selector for a UIButton created in drawRect by using UIControlEventTouchDown instead of the popular UIControlEventTouchUpInside.

查看更多
再贱就再见
5楼-- · 2020-04-02 09:46

Does the new view accept user interaction? In other words, is userInteractionEnabled enabled on the view "Characters"?

查看更多
家丑人穷心不美
6楼-- · 2020-04-02 09:48

FINALLY!!!!!!

I had to init all the views with initWithFrame and pass in valid frame rects. Init should be used with controllers and initWithFrame passing rects for UIViews!!!!

characterView = [ [ CharacterView alloc ] initWithFrame:CGRectMake(0, 0, 480, 320)]; 
then 
characterMale = [ [ CharacterMale alloc ] initWithFrame:frame];
查看更多
女痞
7楼-- · 2020-04-02 09:48

For me the issue was caused because, the origin of the button frame was bigger then the parent frame.

查看更多
登录 后发表回答