动态创建子类iphone
我正在创建一些通用类。
我创建了三个视图控制器,并创建了一个枚举
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将 id (指针)转换为 UIViewController (或其任何子类)。
您可以将 UIViewController 更改为您自己的类。
You can just cast the id (pointer) to an UIViewController (or any of it's subclasses).
You can change the UIViewController to your own class.
我认为你可以转换成你想要的类型。例如:
你可以这样尝试。
I think you can cast to the type which you want. For example:
You can try in this way.