UIButton programming without using interface builder

Discussion in 'iPhone/iPad Programming' started by chhoda, Dec 2, 2008.

  1. chhoda, Dec 2, 2008
    Last edited by a moderator: Nov 6, 2010

    macrumors 6502

    Joined:
    Oct 25, 2008
    #1
    I am required to make a view where number of buttons to be displayed will depend upon the argument to init. I want to create click event handlers for them dinamically too .. how can i make it possible ? using interface builder is out of question, obviously

    CH

    on start i tried doing this inside my view..

    UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
    btn.center = self.center;
    [btn setTitle:mad:"Ok" forState:UIControlStateNormal];


    why is not the button displaying ?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Because you've not added the button to any visible view.
     
  3. chhoda, Dec 2, 2008
    Last edited by a moderator: Nov 6, 2010

    thread starter macrumors 6502

    Joined:
    Oct 25, 2008
    #3
    also tried buttonWithType ... no luck :(

    Thanks,

    I have done that ... i did not paste the whole code..

    - (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    // Initialization code
    [self setBackgroundColor:[UIColor whiteColor]];
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.center = self.center;
    [btn setTitle:mad:"Ok" forState:UIControlStateNormal];
    [self addSubview:btn];
    }
    return self;
    }

    still it is not displaying

    regards
    CH
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    That makes no difference: you have not added the button to a visible view in a visible window.

    You should ensure you have read and understood the basic documentation, in this case the Window and Views section of the iPhone Application Programming Guide that all iPhone developers should have read all of.
     
  5. thread starter macrumors 6502

    Joined:
    Oct 25, 2008
    #5
    !

    Let me paste the whole code ..

    ___________

    #import "CustomAlert.h"


    @implementation CustomAlert


    - (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    // Initialization code
    [self setBackgroundColor:[UIColor whiteColor]];
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.center = self.center;
    //[btn setTitle:mad:"Ok" forState:UIControlStateNormal];
    [self addSubview:btn];
    }
    return self;
    }


    - (void)drawRect:(CGRect)rect {
    // Drawing code
    }


    - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {

    }

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


    @end


    ___________


    I call this custom dialog like

    // UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:self cancelButtonTitle:mad:"XXX" otherButtonTitles:mad:"YYY", @"ZZZ", nil];
    //
    // [myAlert show];
    CustomAlert* alert = [[CustomAlert alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    alert.userInteractionEnabled = YES;
    [self.superview addSubview:alert];

    the commented code is how i was doing, but i am needed to do it through custom alert

    now essentially this customalert is a view which is a sub view of already visible view. And my button is a sub view under custom alert. So what's the problem ? Should not it show the button at least ?

    moreover the white customalertview is displayed. but the button is not ...



    CH
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Is the view you are adding the button to visible? If you look at the UICatalog sample code you can see how Apple create buttons (and most other UI elements) programatically. In particular the ButtonsViewController.m file might help you
     
  7. thread starter macrumors 6502

    Joined:
    Oct 25, 2008
    #7
    yes

    Yes the buttom I am adding the view to is visible with white colour. But surprisingly enough the button is not visible..

    I saw the UICatalog application also does similar thing...

    - (void)setView:(UIView *)inView
    {
    if (view)
    [view removeFromSuperview];
    view = inView;
    [self.view retain];
    [self.contentView addSubview:inView];

    [self layoutSubviews];
    }

    it adds the button to cell's contentview as a subview..

    Help .... ... .......
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    In which method are you defining and add the CustomAlert? Also, why is it not a subclass of UIView? And what is self.superview at this point? Perhaps you want self.view or even window?
     
  9. thread starter macrumors 6502

    Joined:
    Oct 25, 2008
    #9
    got it

    bit strange, but when i did like btn.frame = CGRectMake(0,0, 20, 20);

    it worked..
    no other changes in the above code...
     
  10. macrumors regular

    Joined:
    Dec 1, 2007
    #10
    Maybe the OS is too smart for our own good... a 10x10 pixel button would be extremely small on the screen and very hard to press. It's possible that the iPhone wont even render them below a certain size.
     
  11. macrumors newbie

    Joined:
    Oct 8, 2008
    #11
    Be sure to retain when using static helper method

    I struggled for a couple hours on this one...

    Code:
    	CGRect buttonRect = CGRectMake(0, 0, 200, 40); // whatever frame you need
    	UIButton *logInRegisterButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];  // there's the retain needed
    // UIButton *logInRegisterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; << this will not work because the release following adding to subview destroys it
    	[logInRegisterButton setFrame:buttonRect];	
    	[logInRegisterButton setTitle:@"Log In or Register" forState:UIControlStateNormal];
    	[[self view] addSubview:logInRegisterButton];
    	[logInRegisterButton release];
    
     
  12. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Not sure about that. I thought the addSubview: does its own retain of the passed view.
     
  13. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #13
    It does.
     
  14. macrumors newbie

    Joined:
    Feb 29, 2004
    Location:
    Upstate New York
    #14
    I'm sure someone will correct me if I'm wrong, but I think you'll have a memory leak doing it this way. I don't think you want to explicitly retain your button when you create it, since it will be autoreleased for you. Since it is being autoreleased, you do not want to explicitly release it either. This should cause your button to be dealloc'd properly. But I'm fairly new to all this, so someone can slap me if I'm wrong. ;)

    Edit: On second look, it probably works, there's just an extra retain/release?
     
  15. macrumors newbie

    Joined:
    Sep 21, 2010
    #15
    maybe..

    maybe you forgot to set the color of the button's text...


    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    i had that problem.
     
  16. macrumors 6502a

    Joined:
    Nov 9, 2007
    #16
    I'm pretty sure a button initialise with a type will have a 0x0 frame which is why you were not seeing it until you gave it a bigger frame.
     
  17. macrumors newbie

    sameza

    Joined:
    Nov 6, 2010
    Location:
    Russia->Vladivostok

Share This Page