views:

20

answers:

1

I am repeating my codes few times to create custom buttons, I am trying to create a method and create all my required buttons just by called that method and using two parameters, btnTitle and btnAction.

My Codes for the method

-(void) addnewButton:(NSString *) btnTitle withAction:UIAction btnAction; {

  // Add a custom edit navigation button
 editButton = [[[UIBarButtonItem alloc]
   initWithTitle:NSLocalizedString((btnTitle), @"")
   style:UIBarButtonItemStyleBordered
   target:self
   action:@selector(btnAction)] autorelease];
 self.navigationItem.rightBarButtonItem = editButton;

}

Now how shall I call this method to create a button?

A: 

Hello Not quite sure what the question is here?

When the design of an App is sort of laid out, I have build a ISInterfaceElements Class that holds stuff I will be needing around the app. (UIlabels, UIColors, UIButtons etc.) the instantiation of UIElements is just taking up space, complicating matters and if you need to change a button or color that is used in 14 places it is really bad to have written all the setup code in different places. Anyways, I usually do it like this:

//  ISInterfaceElement.h


#import <Foundation/Foundation.h>

typedef enum {

    DarkBackground, 
    LightBackground,


} ISColorType;

typedef enum {

    Headline, 
    Detail,


} ISLabelType;

@interface ISInterfaceElement : NSObject {

}

+ (UIColor*) getColor:(ISColorType) colorType;
+ (UILabel*) getLabel:(ISLabelType) labelType;

@end

With nice enums to make it easy to remember.

//  ISInterfaceElement.m

#import "ISInterfaceElement.h"


@implementation ISInterfaceElement

+ (UIColor*) getColor:(ISColorType) colorType {

    int value;

    switch (colorType) {

        case DarkBackground:
            value = 0x2d3c3f;
            break;          
        case LightBackground:
            value = 0x627376;
            break;
        default:
            value = 0x000000;
            break;
    }

    int r, g, b;
    b = value & 0x0000FF;
    g = ((value & 0x00FF00) >> 8);
    r = ((value & 0xFF0000) >> 16);

    return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0f]; 


}

+ (UILabel*) getLabel:(ISLabelType) labelType {

    UILabel *label = [[[UILabel alloc] init] autorelease];
    [label setBackgroundColor:[UIColor clearColor]];    

    switch (labelType) {
        case Headline:
            [label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14]];
            [label setTextColor:[UIColor whiteColor]];
            break;
        case Detail:
            [label setFont:[UIFont fontWithName:@"HelveticaNeue" size:14]];
            [label setTextColor:[UIColor whiteColor]];
            break;  
        default:
            break;
    }

    return label;
}

Because these are all Class methods I don't need to instantiate the ISInterfaceElement class to use them.

I just go:

UILabel aHeadlineLabel = [ISInterfaceElement getLabel:Headline];

You can build something like that for your buttons, all you need to do is build a new UIButton inside your method, set the title and action and lastly return myButton, mush like I do with the labels.

Hope it helps

RickiG