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?
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; }
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]; }
You can use the category built-in feature in Objective-c :
1. Apple documentary for customising classes
2. Categories tutorial
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.