views:

3848

answers:

3

I have an view in my App which has a number of buttons based on the number of items returned by the server. So if the server returns say 10 items, there should be 10 buttons and clicking on each button should call a different person.

For the above purpose I created a custom button class deriving from UIButton.

@implementation HopitalButton

@synthesize index;
@synthesize button_type;

- (id)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
     UIImage* img = [UIImage imageNamed:@"dr_btn.png"];
     [img stretchableImageWithLeftCapWidth:10 topCapHeight:10];
     [self setBackgroundImage:img forState:UIControlStateNormal];
     [self setTitleColor:[UIColor colorWithRed:0.698 green:0.118 blue:0.376 alpha:1] forState:UIControlStateNormal] ;
     [self setFont:[UIFont fontWithName:@"Helvetica Bold" size:13]];
     self.titleLabel.textColor = [UIColor colorWithRed:178 green:48 blue:95 alpha:1];
     self.adjustsImageWhenHighlighted = YES;
    }
    return self;
}

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


@end

Now the problem with the above code is that it does not create buttons that look similar to the buttons created by default in Interface builder. The borders are missing.

And I create buttons of the above type by the following code:

 HopitalButton* hb = [[HopitalButton alloc] init];
 hb.button_type = @"call";
 hb.frame = CGRectMake(50, 50 + i * 67, 220, 40);
 [self.scroll_view addSubview:hb];
 [hb setTitle:[[[self.office_full_list objectAtIndex:i] objectForKey:@"Staff" ]objectForKey:@"FullName"] forState:UIControlStateNormal];
 hb.index = [NSNumber numberWithInt:[self.button_items count]];
 [self.button_items insertObject:hb atIndex:[self.button_items count]];
 [hb addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

I am not finding a way to set the button type for this custom button. Is there a way i can do it ? Or is there a better way to design the code.

+3  A: 
coneybeare
But the index is important for my program as I use the index to find out which button was pressed since there are more than one button in the view
Amal
set a tag to the button. I updated the code
coneybeare
Where did you get the stretchable red button image?
bentford
I think i grabbed it off a tutorial somewhere a long time ago… but it shouldn't be too hard to make in photoshop
coneybeare
+1  A: 

Shouldn't you be calling initWithFrame: rect instead of:

    HopitalButton* hb = [[HopitalButton alloc] init];
mahboudz
A: 
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]]; 

setFont is now deprecated, use titleLabal.font property instead

sampleButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
Peter Pajchl