-1

ビューの名前を 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"]];

ビュー名に応じて、ラベルに異なるテキストを表示したいと考えています。したがって、ケースがビューの名前であるスイッチが必要です。ビューの名前を返す方法がわからないので、それらをチェックできます。

ありがとう。

4

2 に答える 2

1

switch ([num intValue]) {ViewControllerの名前の数値部分を表すNSNumberのintValue()を切り替えて、次のように名前を付けることができます(以前に設定したNSNumberの「num」を想定)。

[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"View%d", num]]];
于 2012-10-22T18:57:21.740 に答える
1

次のエラーが発生しない限り、NSStringをObjectiveCのswitchステートメントに渡すことはできません。

エラー:ステートメントには整数型の式が必要です('NSString *'は無効です)

代わりにifステートメントを使用して、isEqualToStringを確認してください

于 2012-10-22T18:59:11.177 に答える