【问题标题】:Pass storyboard names into a switch statement?将故事板名称传递到 switch 语句中?
【发布时间】:2012-10-22 18:48:45
【问题描述】:

我可以将视图的名称作为 switch 语句的参数吗?如果是这样,我将在 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"]];

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

谢谢。

【问题讨论】:

  • 如果你的名字在一个字符串中,你就不需要开关了。如果您试图在运行时获取 变量 的标签,那么您可以为 ivars 执行此操作,但几乎可以肯定这是错误的方法。请详细说明您要完成的工作。

标签: objective-c uiviewcontroller storyboard switch-statement


【解决方案1】:

您不能将 NSString 传递给目标 C 中的 switch 语句而不会出现以下错误。

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

改为使用 if 语句并检查 isEqualToString

【讨论】:

  • 我如何获得视图的名称来检查这个?
  • 它实际上不是视图的名称,它是您在界面生成器中设置的segue的标识符。 [segue identifier] 将从 segue 中获取名称。
  • 是的,它是在 ib 的 Storyboard id 中定义的。还是不能退货
  • 如果 (controlleridentifer name == View1) then title = title1. elseif (viewcontrolleridentifer name = View2) then title = title2 依此类推。
【解决方案2】:

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

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

【讨论】:

  • 无法开启对象,只能开启整数类型。
  • 嗯,我不记得说开关会是'[num intValue]',例如。
猜你喜欢
  • 1970-01-01
  • 2015-10-05
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 2012-01-15
  • 2021-04-16
  • 1970-01-01
  • 2012-03-31
相关资源
最近更新 更多