Iphone UIButton not working in nested UIViews
Solution 1
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];
Solution 2
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.
Solution 3
Does the new view accept user interaction? In other words, is userInteractionEnabled enabled on the view "Characters"?
Solution 4
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];
}
}
Solution 5
I have had luck with a selector for a UIButton created in drawRect by using UIControlEventTouchDown
instead of the popular UIControlEventTouchUpInside
.
claudia smith
Updated on July 09, 2022Comments
-
claudia smith almost 2 years
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
-
claudia smith almost 15 yearsI thought I had read userInteractionEnabled by default is YES. I would think in this situation I would make userInteractionEnabled = NO for the CharactersView class. I think this view is somehow blocking the button in my CharactersMale class. In any case I tried and no difference.
-
mahboudz almost 15 yearsI have a view that contains a bunch of buttons. I addView a new view with a few of its own buttons over the ones that were there. All buttons work, including the ones between the first layer and the second (top) layer. So I you should have no problems nesting buttons. But if I turn off userInteraction in the superLayer, all buttons stop working. If I only turn off userInteraction in the top layer, it only disabled the button on that layer. Keep at it. I bet you'll find a simple error or so.
-
Emil over 13 years@Roger Because others might have the same problem. You don't just delete a question when it's resolved...
-
Wayne Lo about 13 yearsKay, Thank you for the advice. I set UIImageView suerInteractionEnabled to yes and it worked.