Iphone UIButton not working in nested UIViews!!! Ugh

Discussion in 'iOS Programming' started by cpiv, May 4, 2009.

  1. macrumors newbie

    Joined:
    May 4, 2009
    #1
    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?

    CharacterView.m is being used as a container.


    PHP:
    /////////////////////////////////////////////////////////////////////////////////
     
    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 addSubviewcharacterMale];
            
            
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            
    button.frame CGRectMake(0200200100);
            [
    button setImage:[UIImage imageNamed:@"btnCharSelect.png"forState:UIControlStateNormal];
            [
    button addTarget:self action:@selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];
            [ 
    self addSubviewbutton ];
        }
        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 allocinitWithImage:image];
            [
    image release];
            [ 
    self addSubviewimageView ];
            
            
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            
    button.frame CGRectMake(00200100);
            [
    button setImage:[UIImage imageNamed:@"btnCharSelect.png"forState:UIControlStateNormal];
            [
    button addTarget:self action:@selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];
            [ 
    self addSubviewbutton ];

        }
        return 
    self;
    }

    -(
    void)ApplyImage:(id)sender
    {
        
    NSLog(@"CharacterMal button works");
    }

    - (
    void)dealloc {
        [
    super dealloc];
    }

    @
    end
     
  2. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    You should init all these views with initWithFrame and you should pass in valid frame rects.
     
  3. thread starter macrumors newbie

    Joined:
    May 4, 2009
    #3
    I will do that. Is this required or just good practice? I was under the impression that instantiated classes within UIView would be initialized with its parents view port.

    Either way I will try that and see if that is the answer to my annoying issue.
     
  4. thread starter macrumors newbie

    Joined:
    May 4, 2009
    #4
    DUDE THAT WAS IT!!!!!!!! I can't believe i wasted that much time! Thanks so much!
     

Share This Page