将故事板名称传递给switch语句?

时间:2012-10-22 18:48:45

标签: objective-c uiviewcontroller storyboard switch-statement

我可以将视图的名称作为switch语句的参数吗?如果是这样,我会在开关中使用什么?

[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View1"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View2"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View3"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View4"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View5"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View6"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View7"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View8"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View9"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View10"]];

根据视图名称,我想在标签中显示不同的文本位。所以我想要一个开关,其中案例是视图的名称,我不知道如何返回视图的名称,以便我可以检查它们。

感谢。

2 个答案:

答案 0 :(得分:1)

你可以切换一个代表ViewController名称的数字部分的NSNumber的intValue(switch ([num intValue]) {),然后将其命名为(假设你的NSNumber之前设置的'num'):

[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"View%d", num]]];

答案 1 :(得分:1)

如果没有收到以下错误,则无法将NSString传递给目标C中的switch语句。

  

错误:语句需要表达整数类型(' NSString *'无效)

改用if语句并检查isEqualToString