Iphone UIButton not working in nested UIViews

It's so damn simple, of course! I missed something, and I got scared trying to fix it. hope someone can help.

The button in CharacterView.m works, but the button nested in CharacterMale.m does not work. I do not use IB, everything is done progastically. What can make one button work, but not the other?

///////////////////////////////////////////////////////////////////////////////// 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 
+11
iphone uiviewcontroller uibutton uiview
May 04 '09 at 4:01 a.m.
source share
6 answers

FINALLY!!!!!!

I had to initialize all the views with initWithFrame and pass the actual frame rectangles. Init should be used with controllers and initWithFrame passing lines to UIViews !!!!

 characterView = [ [ CharacterView alloc ] initWithFrame:CGRectMake(0, 0, 480, 320)]; then characterMale = [ [ CharacterMale alloc ] initWithFrame:frame]; 
+17
May 04 '09 at 8:28 a.m.
source share

Another thing to keep in mind is that the UIButton subviews that were added, the UIImageView does not work at all. You need to create a UIView in which you add the image and buttons.

This is probably due to disabling the default interaction when viewing images, but I did not check this.

+17
May 24 '10 at 11:20
source share

Does the new view affect user interaction? In other words, is userInteractionEnabled enabled in the Character view?

+2
May 04 '09 at 8:30
source share

I had a similar problem when trying to add a button while initializing a UIView with a CGRectZero frame:

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

As soon as I changed the frame to the correct 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]; } } 
+1
Jun 13 '10 at 15:04
source share

I got lucky with a selector for a UIButton created in drawRect using UIControlEventTouchDown instead of the popular UIControlEventTouchUpInside .

+1
Jan 31 '11 at 20:30
source share

For me, the problem was caused by the fact that the origin of the button frame was larger than the parent.

+1
Jul 26 '13 at 20:50
source share



All Articles