动态创建子类iphone

发布于 2024-11-02 02:32:36 字数 862 浏览 1 评论 0原文

我正在创建一些通用类。

我创建了三个视图控制器,并创建了一个枚举

typedef enum Type {
    type1,
    type2,
    type3,
} Type;

,对于三种不同的类型,我需要使用不同的视图控制器子类。 但用户感觉就像一个。 (我的意思是我可以在我们需要决定使用哪个控制器的接口类中为其提供一个接口)。

我尝试这样, 我创建一个类并重写 init 方法,如下所示,

- (id)initWithReaderType:(ReaderType )readerType    {

        switch (readerType) {
            case 0: {
                Slider *slider = [[Slider alloc] init];
                return slider;
            }
            case 1: {
                //use other controller
                break;
            }
            default:    {
                NSLog(@"Exception on Initialization : Un Recognized Reader Type");
                break;
            }
        }

    return nil;
}

但它作为 id 对象返回,所以我不能将它用作视图控制器对象,并且我无法访问该控制器类的属性和方法。

我怎样才能实现这个目标?

提前致谢....

I am in the middle of creating some generic class.

And I create three view-controller and I create a enum

typedef enum Type {
    type1,
    type2,
    type3,
} Type;

and for three different type I need to use different view-controllers subclass.
But the user feels like one. (what I mean is I can provided it with an interface in that interface class we need to decide which controller used).

I try like this,
I create a class and override the init method like follows,

- (id)initWithReaderType:(ReaderType )readerType    {

        switch (readerType) {
            case 0: {
                Slider *slider = [[Slider alloc] init];
                return slider;
            }
            case 1: {
                //use other controller
                break;
            }
            default:    {
                NSLog(@"Exception on Initialization : Un Recognized Reader Type");
                break;
            }
        }

    return nil;
}

But it returns as id object, So I can't use it as a view-controller object and I don't get access to the properties and methods of that controller class.

How Can I achieve this?

thanks in advance....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞琼窗梦回ˉ 2024-11-09 02:32:36

您可以将 id (指针)转换为 UIViewController (或其任何子类)。

id result = [self initWithReaderType:type1];
// Check if we can cast.
if ([result isKindOfClass[UIViewController class]]) {
   UIViewController *viewController = (UIViewController *)[self initWithReaderType:type1];
}

您可以将 UIViewController 更改为您自己的类。

You can just cast the id (pointer) to an UIViewController (or any of it's subclasses).

id result = [self initWithReaderType:type1];
// Check if we can cast.
if ([result isKindOfClass[UIViewController class]]) {
   UIViewController *viewController = (UIViewController *)[self initWithReaderType:type1];
}

You can change the UIViewController to your own class.

荒岛晴空 2024-11-09 02:32:36

我认为你可以转换成你想要的类型。例如:

Slider *incomeSlider = (Slider *)[self initWithReaderType:type1];

你可以这样尝试。

I think you can cast to the type which you want. For example:

Slider *incomeSlider = (Slider *)[self initWithReaderType:type1];

You can try in this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文