views:

89

answers:

1

Hi guys,

I need to generate a custom button through code, this is how i am currently doing it.

-(void) initialiseButtons 
{
    int ypos = playerImage.frame.origin.y + playerImage.frame.size.height + 8;
    for(int i=0; i<totalButtons; i++) 
    {       
        UIButton *newButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
        [newButton setFrame:CGRectMake(20, ypos, 220, 30)];
        newButton.tag = 10 + i;
        [newButton addTarget:self action:@selector(statTapped:) forControlEvents:UIControlEventTouchUpInside];
        [self.frontView addSubview:newButton];
        ypos += 30 + 7;
    }
}

This creates the blank buttons perfectly through code, gives them a tag and assigns an callback function on touchUpInside.

The custom button needs to be able to handle showing an image when pressed down. It needs to be able to draw 2 pieces of Text. 1 aligned to the left hand side of the button and 1 aligned to the righthand side of the button.

My boss suggested instead of buttons I use a View. I dont understand how this will work. When i start thinking about it, i think it would require having a viewcontroller dedicated to the buttons. And some draw method? It sounds complicated and I am not grasping how it can be done.

Is there a simpler method by making a custom class overriding UIButton? I made a simple test class earlier but nothing was drawn in the buttons locations when I used them in place of the Normal UIButton class. I expected i would see the buttonUp.png drawn.

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface CardButton : UIButton {
}
@end


#import "CardButton.h"
#import <UIKit/UIKit.h>
@implementation CardButton
- (id)initWithFrame:(CGRect)frame 
{
    if (self = [super initWithFrame:frame]) 
    {
        [self setImage:[UIImage imageNamed:@"buttonUp.png"] forStates:UIControlStateNormal];
        self.adjustsImageWhenHighlighted = YES;
    }
    return self;
}
@end

Anybody able to give me some pointers on this? I'm pretty blocked at the moment. Have been reading some various other threads related to buttons but nothing that made it clear in my head how to tackle the problem

+1  A: 

You should not (I'd almost say you can't) subclass UIButton, as it turns out to be a class cluster, and it would be impractical (read: impossible) to derive from that.

So your two options are:

1) Use standard UIButton of custom type, add the elements you want to show (i.e. UILabel) and hook up actions for touch down and touch up and react accordingly (change the views, trigger actions etc.)

2) Use a custom UIView, implement drawRect: to draw how you like it or use custom views as subviews as in 1). Then use the touchesBegan:, touchesEnded: etc. messages to react accordingly (or UIGestureRecognizer if you can live with 3.2+ compatibility).

You might build a factory to build those buttons if they all are very similar, so that your code becomes clean and non-repetive.

Eiko
CAnt figure out how to do this. Gonna leave it 24hours and see if it makes more sense when i come back to it. THanks for the help Eiko its appreciated :)
Code