动态设置内联 UIViewController 指针 - Objective C
Dynamically set UIViewController pointer inline - Objective C
我有一个带有 for 循环的 iOS 应用程序,可以创建、设置自定义视图控制器并将其添加到我的视图中。问题是我需要根据当前循环数将 UIViewController
对象动态设置为正确的 class。这是我的代码:
// 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]];
}
问题是上面的一些方法调用出现了错误:
No visible @interface for 'UIViewController' declares the selector....
为了解决这个问题,我需要对对象进行类型转换screen
。但是,我需要根据 for 循环编号对其进行动态类型转换:
如果循环小于3,我需要将对象类型转换为CustomSwitchView
,否则我需要将其类型转换为CustomTripleSwitchView
。我怎样才能做到这一点?例如,我尝试了下面的代码,但没有成功:
(loop < 3 ? (CustomSwitchView *) : (CustomTripleSwitchView *))
有几种方法可以解决这个问题。对现有代码影响最小的是将方法区分为一般适用于 UIViewController 的方法和特定于 subclasses 的方法。对声明为特定 subclass...
的堆栈变量调用 subclass 方法
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) 我应该为每个 class 定义一个协议(正如评论者恰当地建议的那样),或者 (b) 这些真的是 subclass 彼此相关,例如 CustomTripleSwitchView
实际上是 CustomSwitchView
的子 class?
我有一个带有 for 循环的 iOS 应用程序,可以创建、设置自定义视图控制器并将其添加到我的视图中。问题是我需要根据当前循环数将 UIViewController
对象动态设置为正确的 class。这是我的代码:
// 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]];
}
问题是上面的一些方法调用出现了错误:
No visible @interface for 'UIViewController' declares the selector....
为了解决这个问题,我需要对对象进行类型转换screen
。但是,我需要根据 for 循环编号对其进行动态类型转换:
如果循环小于3,我需要将对象类型转换为CustomSwitchView
,否则我需要将其类型转换为CustomTripleSwitchView
。我怎样才能做到这一点?例如,我尝试了下面的代码,但没有成功:
(loop < 3 ? (CustomSwitchView *) : (CustomTripleSwitchView *))
有几种方法可以解决这个问题。对现有代码影响最小的是将方法区分为一般适用于 UIViewController 的方法和特定于 subclasses 的方法。对声明为特定 subclass...
的堆栈变量调用 subclass 方法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) 我应该为每个 class 定义一个协议(正如评论者恰当地建议的那样),或者 (b) 这些真的是 subclass 彼此相关,例如 CustomTripleSwitchView
实际上是 CustomSwitchView
的子 class?