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
I had a similar issue when tried to add the button during the initialization of an UIView with a frame of CGRectZero:
Once I changed the frame to a proper rectangle, the button worked:
Another thing to consider is that
UIButton
subviews that have been added aUIImageView
don't work at all. You need to create aUIView
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.
I have had luck with a selector for a UIButton created in drawRect by using
UIControlEventTouchDown
instead of the popularUIControlEventTouchUpInside
.Does the new view accept user interaction? In other words, is userInteractionEnabled enabled on the view "Characters"?
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!!!!
For me the issue was caused because, the origin of the button frame was bigger then the parent frame.