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;
}
Is there anything wrong/bad with this implementation?
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