我将以编程方式创建 UIButton 和其他视图。按钮将在自定义类调用 ButtonBuilder 中创建。这个构建器类将是 BaseBuilder 类的子类。类如下所示。
BaseBuilder 类
- (id)init{
self = [super init];
if (0 != self) {
baseView = [[UIView alloc]init]; //baseView is a property of BaseBuilder class
}
return self;
}
//Real implementation of build class is more complicated than just setting
//the background color and frame size
-(void)build{
baseView.backgroundColor = [UIColor clearColor];
baseView.frame = CGRectMake(0,0,50,50);
}
ButtonBuilder 类
-(void)build:(UIView*) view{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[super build];
button = (UIButton*)baseView;
button.text = @"ButtonText"; // other button settings will be added here
[view addSubview:button]; // view is view from main view controller
}
我知道在 iOS 中无法将 UIView 转换为 UIButton 。因此,我需要关于替代意见的建议。