简体   繁体   English

在超类数组中初始化UIViewController子类?

[英]Initialize UIViewController subclasses in array of superclass?

Here's my setup. 这是我的设置。 I subclassed UIViewController once to create a single "Widget" view controller that handles everything that all of my widgets can do. 我将UIViewController子类化了一次以创建一个“Widget”视图控制器,它可以处理我所有小部件都可以执行的操作。 I then subclassed the Widget View Controller for each of my widgets, since they each handle their own unique and often complex set of functions. 然后我为每个小部件子类化了Widget视图控制器,因为它们每个都处理它们自己独特且通常很复杂的功能集。

I want to create an array of Widgets that stores each active widget in my scene, however I'm having trouble trying to figure out how to initialize the proper widget to store in the array. 我想创建一个Widgets数组,用于存储场景中的每个活动窗口小部件,但是我无法弄清楚如何初始化要存储在数组中的正确窗口小部件。 Initially I figured I could just do [widgets addObject:[[Widget alloc] initWithNibName:widgetName bundle:nil]]; 最初我想我可以做[widgets addObject:[[Widget alloc] initWithNibName:widgetName bundle:nil]]; , however that completely ignores the individual widget initWithNibName function and goes right to the abstracted Widget class (which makes sense, since that's the class that I'm loading it into...). 然而,这完全忽略了单个小部件initWithNibName函数,并直接进入抽象的Widget类(这是有道理的,因为那是我正在加载它的类......)。

How can I get my classes loaded into this array properly with each widget being initialized by its own unique subclass? 如何通过自己唯一的子类初始化每个小部件,如何正确地将我的类加载到此数组中? Feel free to recommend structural changes as well, if necessary. 如有必要,请随意推荐结构变化。

If the name of the class is the same as the nib 如果类的名称与nib相同

NSArray* widgets = [NSArray arrayWithObjects: @"MyVC1",@"MyVC2",nil];

NSString* newVC = [widgets objectAtIndex: value];

Class classVC = NSClassFromString(newVC);

Widget* controller = [[classVC alloc] initWithNibName:newVC bundle:nil];

Well instead of this: 好吧而不是这个:

[widgets addObject:[[Widget alloc] initWithNibName:widgetName bundle:nil]];

You can do this: 你可以这样做:

MySpecifc *mySpecificWidget = [[MySpecifc alloc] initWithNibName:widgetName bundle:nil];
[widgets addObject:myWidget];

My question here is, where is this NSMutableArray stored? 我的问题是,这个NSMutableArray存储在哪里?

Myabe have a Singleton so you can keep track of them? Myabe有一个Singleton ,你可以跟踪它们吗?

I had a similar problem and I resolved the problem using a function like this in the "abstract" Widget class: 我有一个类似的问题,我在“抽象”Widget类中使用这样的函数解决了问题:

+ (Widget)InstanceFor(widgetType)type {
    switch(type) {
        case widgetTypeWidgetOne:
            return [[WidgetOne alloc] initWithNibName:@"WidgetOneName" bundle:nil] autorelease];
            break;
        case widgetTypeWidgetTwo:
            return [[WidgetTwo alloc] initWithNibName:@"WidgetTwoName" bundle:nil] autorelease];
            break;
    }
}

Where widgetType is this: 其中widgetType是这样的:

typedef enum {
    widgetTypeOne,
    widgetTypeTwo
} widgetType;

So you can store Widget object in the array and cast it when you need it. 因此,您可以将Widget对象存储在数组中,并在需要时将其强制转换。 When you want to initialize a widget you can do that: 当您想要初始化小部件时,您可以这样做:

[widgets addObject:[Widget InstanceFor:widgetTypeWidgetOne]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM