views:

717

answers:

4

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.

CharacterView.m is being used as a container

/////////////////////////////////////////////////////////////////////////////////
 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 {
    [characterView release];
    [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 {
    [characterMale release];
    [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
A: 

I'm new to this too, but have you tried: UIControlEventValueChanged instead of UIControlEventTouchUpInside ?

Will Hartung
That didnt work :( Im dying here!
Charles Peterson
A: 

How big is the charMale.png image? What is the z-Order for the image and button in CharacterMale? The image may be sitting on top of the button that you are creating preventing it from being touched.

Hopefully this is the problem... I've not seen anything else yet...

Frank V
I have commented out the image loading in CharacterMale.m and still have the same problem:( Thanks though
Charles Peterson
Long shot, but could you post the header files too? Also, if you do a clean rebuild are there any warnings?
Frank V
I resolved it!!!!
Charles Peterson
+1  A: 

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];
Charles Peterson
I'm glad. Sorry I couldn't have been of more help but I am glad you solved it.
Frank V
As long as I can move onto the next appending problem that's bound to happen :) LOL
Charles Peterson
A: 

u are sure???? u made commections with file ownwer

Yes everything works perfect. That was the problem.
Charles Peterson