ios - UIView工厂创建过程

标签 ios uiview delegates factory

我有一个将绘制图表的 View ......将会有不止一种图表类型......我如何将它们包装在工厂类中?所以我有一个 MyChart 类(UIView 子类)...如果用户可以做类似的事情:

MyChart *chart = [[Mychart alloc] initWithFrame:frame andType:1];

图表实际上将变成一个 MyLineChart 对象……或者 myChart 可以创建的其他对象? 我是否只是在 myChart 类的初始化中创建 thouse View ......然后返回它们而不是返回自己?

最佳答案

像这样的伪代码

typedef enum {
    MyChartTypeLine,
    MyChartTypePoint,
    ...
} MyChartType;

MyLineChart *chart = [MyChart chartWithType:MyChartTypeLine];

在MyChart.m中添加一个类方法:

+ (id)chartWithType:(MyChartType)type
{
    switch (type) {
        case MyChartTypeLine:
            return [[MyLineChart alloc] init];
        case...
    }
    return nil;
}

关于ios - UIView工厂创建过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13440406/

相关文章:

iphone - 是否可以通过 nsnotification 将数组作为 obj 传递

iOS 游戏中心错误 : status = 5008, 缺少必需的 header key :x-gk-bundle-version

ios - 从字节中获取位的值 | swift 5

iOS:self.view.bounds 没有填满整个窗口

c# - 如何创建算法类型?

c# - 事件处理程序引发方法约定

ios - AVAssetExportSession exportAsynchronouslyWithCompletionHandler 返回失败

ios - 根据百分比用颜色填充 UIView

ios - 自定义 View 和属性 'v' 实例变量

ios - GameCenter 通知干扰 iAd View