Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have been working on automating some aspects of creating UI elements and have created this method that seems to work:

    - (void) viewDidLoad
        {
            [super viewDidLoad];
            button = [self createButton:button xPos:30 yPos:100 width:100 height:30  caption:@"autoButton" textPos:NSTextAlignmentCenter textClr:[UIColor blackColor] backClr:[UIColor yellowColor]];
            [self.view addSubview: button];
        }

    - (UIButton *) createButton:(UIButton *)control xPos:(CGFloat)x yPos:(CGFloat)y width:(CGFloat)width height:(CGFloat)height caption:(NSString *)caption textPos:(NSTextAlignmentCenter)textPosition textClr:(UIColor *)textColor backClr:(UIColor *)backColor
        {
            control = [[UIButton alloc] initWithFrame: CGRectMake(x, y, width, height)];
            [control setTitle:caption forState:UIControlStateNormal];
            control.titleLabel.textAlignment = textPosition;
            control.backgroundColor = backColor;
            [control setTitleColor: textColor forState:UIControlStateNormal];
            return control;
        }
  1. Is there anything wrong/bad with this implementation?

  2. Instead of using methods, would it be possible to implement the same thing with just plain old functions? It's a little long-winded to have to indicate the parameter names, xPos:30 yPos:100 width:100 height:100, etc.

Would be nice to be able to just do something like:

button = createButton(30, 100, 100, 30, @"autoButton", NSTextAlignmentCenter, [UIColor blackColor], [UIColor yellowColor]);

Is this doable?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
142 views
Welcome To Ask or Share your Answers For Others

1 Answer

  1. Fine except it makes no sense to pass in the button parameter. Just return a UIButton object.

    - (void) viewDidLoad
        {
            [super viewDidLoad];
            button = [self createButtonWithxPos:30 yPos:100 width:100 height:30  caption:@"autoButton" textPos:NSTextAlignmentCenter textClr:[UIColor blackColor] backClr:[UIColor yellowColor]];
            [self.view addSubview: button];
        }
    
    - (UIButton *) createButtonWithxPos:(CGFloat)x yPos:(CGFloat)y width:(CGFloat)width height:(CGFloat)height caption:(NSString *)caption textPos:(NSTextAlignmentCenter)textPosition textClr:(UIColor *)textColor backClr:(UIColor *)backColor
        {
            UIButton *control = [[UIButton alloc] initWithFrame: CGRectMake(x, y, width, height)];
            [control setTitle:caption forState:UIControlStateNormal];
            control.titleLabel.textAlignment = textPosition;
            control.backgroundColor = backColor;
            [control setTitleColor: textColor forState:UIControlStateNormal];
            return control;
        }
    
  2. Fine, use a function or a method. Up to you. As long as you don't need self involved, a function works.

    UIButton *createButton(CGFloat x, CGFloat y, CGFloat width, CGFloat height, NSString *caption, NSTextAlignmentCenter textPosition, UIColor *textColor, UIColor *backColor) {
        UIButton *control = [[UIButton alloc] initWithFrame: CGRectMake(x, y, width, height)];
        [control setTitle:caption forState:UIControlStateNormal];
        control.titleLabel.textAlignment = textPosition;
        control.backgroundColor = backColor;
        [control setTitleColor: textColor forState:UIControlStateNormal];
        return control;
    }
    
    - (void) viewDidLoad
        {
            [super viewDidLoad];
            button = createButton(30, 100, 100, 30, @"autoButton", NSTextAlignmentCenter, [UIColor blackColor], [UIColor yellowColor]);
            [self.view addSubview: button];
        }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...