Ios 动态设置UIViewController指针内联-目标C

Ios 动态设置UIViewController指针内联-目标C,ios,objective-c,cocoa-touch,uiviewcontroller,casting,Ios,Objective C,Cocoa Touch,Uiviewcontroller,Casting,我有一个iOS应用程序,带有for循环,可以创建、设置并向我的视图添加自定义视图控制器。问题是,我需要根据当前循环编号,将UIViewController对象动态设置为正确的类。这是我的密码: // Loop through the data and setup the switches. for (NSUInteger loop = 0; loop < [switchLabels count]; loop++) { // Create the view

我有一个iOS应用程序,带有for循环,可以创建、设置并向我的视图添加自定义视图控制器。问题是,我需要根据当前循环编号,将
UIViewController
对象动态设置为正确的类。这是我的密码:

// Loop through the data and setup the switches.
    
for (NSUInteger loop = 0; loop < [switchLabels count]; loop++) {
        
    // Create the view controller object.
    UIViewController *screen;
        
    // Create the custom switch view.
        
    if (loop < 3) {
        screen = [[CustomSwitchView alloc] initWithNibName:@"CustomSwitchView" bundle:nil];
    } else {
        screen = [[CustomTripleSwitchView alloc] initWithNibName:@"CustomTripleSwitchView" bundle:nil];
    }
        
    // Create the custom switch view.
    [screen setPassedInType:switchTypes[loop]];
    [screen setDelegate:self];
    [self addChildViewController:screen];
    [screen.view setFrame:CGRectMake((self.view.frame.size.width - 150), ((UILabel *)switchLabels[loop]).frame.origin.y, 144, 72)];
    [scrollTopView addSubview:screen.view];
    [screen didMoveToParentViewController:self];
    [screen setTitles:switchTitles[loop] state:[switchSettings[loop] boolValue]];
}

有几种方法可以解决这个问题。对现有代码影响最小的是将方法区分为一般应用于UIViewController的方法和特定于子类的方法。对声明为特定子类的堆栈变量调用子类方法

for (NSUInteger loop = 0; loop < [switchLabels count]; loop++) {

    // Create the view controller object.
    UIViewController *vc;

    // Create the custom switch view.

    if (loop < 3) {
        CustomSwitchView *screen = [[CustomSwitchView alloc] initWithNibName:@"CustomSwitchView" bundle:nil];
        [screen setPassedInType:switchTypes[loop]];
        [screen setDelegate:self];
        [screen setTitles:switchTitles[loop] state:[switchSettings[loop] boolValue]];
        vc = screen;
    } else {
        CustomTripleSwitchView *screen = [[CustomTripleSwitchView alloc] initWithNibName:@"CustomTripleSwitchView" bundle:nil];
        [screen setPassedInType:switchTypes[loop]];
        [screen setDelegate:self];
        [screen setTitles:switchTitles[loop] state:[switchSettings[loop] boolValue]];
        vc = screen;
    }

    // Create the custom switch view.
    [self addChildViewController:vc];
    [vc.view setFrame:CGRectMake((self.view.frame.size.width - 150), ((UILabel *)switchLabels[loop]).frame.origin.y, 144, 72)];
    [scrollTopView addSubview:vc.view];
    [vc didMoveToParentViewController:self];
}
for(整数循环=0;循环<[switchLabels count];循环++){
//创建视图控制器对象。
UIViewController*vc;
//创建自定义开关视图。
if(循环<3){
CustomSwitchView*屏幕=[[CustomSwitchView alloc]initWithNibName:@“CustomSwitchView”捆绑包:nil];
[screen SetPassedType:switchTypes[loop]];
[屏幕设置代表:自我];
[屏幕设置标题:开关标题[循环]状态:[开关设置[循环]布尔值]];
vc=屏幕;
}否则{
CustomTripleSwitchView*屏幕=[[CustomTripleSwitchView alloc]initWithNibName:@“CustomTripleSwitchView”捆绑包:nil];
[screen SetPassedType:switchTypes[loop]];
[屏幕设置代表:自我];
[屏幕设置标题:开关标题[循环]状态:[开关设置[循环]布尔值]];
vc=屏幕;
}
//创建自定义开关视图。
[self-addChildViewController:vc];
[vc.view setFrame:CGRectMake((self.view.frame.size.width-150),((UILabel*)switchLabels[loop]).frame.origin.y,144,72)];
[scrollTopView添加子视图:vc.view];
[vc didMoveToParentViewController:self];
}

如果我们在项目中遇到这个问题,这是一个不错的解决方案。当你看到这类事情激增时,是时候开始思考:(a)我应该在每个类上定义一个协议(正如一位评论员恰当地建议的那样),还是(b)这些子类真的相互关联,就像
CustomTripleSwitchView
实际上是
CustomSwitchView

的一个子类,如果它们共享您正在使用的一些常用方法,那么您应该声明它们都符合的协议,该协议需要这些方法。然后可以将局部变量声明为
UIViewController*
为什么不在调用实际方法之前尝试使用respondsToSelector?
for (NSUInteger loop = 0; loop < [switchLabels count]; loop++) {

    // Create the view controller object.
    UIViewController *vc;

    // Create the custom switch view.

    if (loop < 3) {
        CustomSwitchView *screen = [[CustomSwitchView alloc] initWithNibName:@"CustomSwitchView" bundle:nil];
        [screen setPassedInType:switchTypes[loop]];
        [screen setDelegate:self];
        [screen setTitles:switchTitles[loop] state:[switchSettings[loop] boolValue]];
        vc = screen;
    } else {
        CustomTripleSwitchView *screen = [[CustomTripleSwitchView alloc] initWithNibName:@"CustomTripleSwitchView" bundle:nil];
        [screen setPassedInType:switchTypes[loop]];
        [screen setDelegate:self];
        [screen setTitles:switchTitles[loop] state:[switchSettings[loop] boolValue]];
        vc = screen;
    }

    // Create the custom switch view.
    [self addChildViewController:vc];
    [vc.view setFrame:CGRectMake((self.view.frame.size.width - 150), ((UILabel *)switchLabels[loop]).frame.origin.y, 144, 72)];
    [scrollTopView addSubview:vc.view];
    [vc didMoveToParentViewController:self];
}