代码之家  ›  专栏  ›  技术社区  ›  Supertecnoboff

动态设置UIVIEW控制器指针内联-C

  •  0
  • Supertecnoboff  · 技术社区  · 6 年前

    我有一个带有for循环的iOS应用程序,它可以创建、设置和添加自定义视图控制器。问题是我需要动态地设置 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”的visible@interface没有声明选择器….

    为了解决这个问题,我需要键入cast对象 screen . 但是,我需要根据for循环数动态地键入cast:

    如果循环小于3,则需要将对象类型转换为 CustomSwitchView ,否则我需要键入cast CustomTripleSwitchView . 我该怎么做?例如,我尝试了下面的代码,但没有成功:

    (loop < 3 ? (CustomSwitchView *) : (CustomTripleSwitchView *))
    

    谢谢你的时间,丹。

    1 回复  |  直到 6 年前
        1
  •  0
  •   danh    6 年前

    有几种方法可以处理这个问题。对现有代码的最小影响将是将方法区分为一般应用于UIViewControllers的方法和那些特别适用于子类的方法。调用被声明为特定子类的堆栈变量的子类方法…

    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];
    }
    

    如果我们在项目中遇到这个问题,这是一个好的解决方案。当你看到这类事情激增时,是时候开始思考:(a)我应该在每个类上定义一个协议(作为一个适当的建议者),还是(b)这些真正的子类彼此相关,比如 CustomTripleSwitchView 真的是 CustomSwitchView 是吗?