我的项目中有几个实用程序类,它们是基于少数参数生成UIElements的工厂类。例如,考虑这是一个很抽象的例子,使用xcode接口生成器的工厂方法
typedef NS_OPTIONS(NSUInteger, MyButtonType)
{
MyButtonRed,
MyButtonBlue,
MyButtonGray,
// ...
}
@interface MyButtonFactory: UIButton
+ (UIButton *)buttonWithType:(MyButtonType)type // mybuttontype is an enum which contains various different type of buttons.
@end
我的问题是我怎么在InterfaceBuilder中使用的UI元素这种方法(例如按钮在这种情况下)?
对于非Interface Builder的元素(代码完成),我只是用它像这样>
UIButton *button = [MyButtonFactory buttonWithType:MyButtonRed];
button.frame = (CGRect){ a, b, c, d };
// etc
感谢
只需在您的界面构建器上放置一个按钮,然后将其类更改为您的自定义类MyButtonFactory – Priyatham51
是的,但是如何获取正确的按钮类型?我的工厂类给出了一个基于传入“buttonWithType:”方法的类型的按钮。 – nsuinteger