Switch语句提出新视图的错误

时间:2014-11-22 03:04:00

标签: objective-c uiviewcontroller switch-statement

我尝试将以下代码切换为switch语句(您可以忽略这些注释):

BEFORE

if (self.players == 1) {
    OnePlayerViewController *onePlayerViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OnePlayerViewController"];
    /*onePlayerViewController.dictionary = self.words;
    onePlayerViewController.wordsText = self.wordsText;*/
    onePlayerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:onePlayerViewController animated:YES completion:nil];
} else if (self.players == 2) {
    TwoPlayersViewController *twoPlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"TwoPlayersViewController"];
    /*twoPlayersViewController.dictionary = self.words;
    twoPlayersViewController.wordsText = self.wordsText;*/
    twoPlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:twoPlayersViewController animated:YES completion:nil];
} else if (self.players == 3) {
    ThreePlayersViewController *threePlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ThreePlayersViewController"];
    /*threePlayersViewController.dictionary = self.words;
    threePlayersViewController.wordsText = self.wordsText;*/
    threePlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:threePlayersViewController animated:YES completion:nil];
} else {
    FourPlayersViewController *fourPlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FourPlayersViewController"];
    /*fourPlayersViewController.dictionary = self.words;
    fourPlayersViewController.wordsText = self.wordsText;*/
    fourPlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:fourPlayersViewController animated:YES completion:nil];
}

AFTER

switch (self.players) {
    case 1:
        OnePlayerViewController *onePlayerViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OnePlayerViewController"];
        /*onePlayerViewController.dictionary = self.words;
        onePlayerViewController.wordsText = self.wordsText;*/
        onePlayerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:onePlayerViewController animated:YES completion:nil];
        break;
    case 2:
        TwoPlayersViewController *twoPlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"TwoPlayersViewController"];
        /*twoPlayersViewController.dictionary = self.words;
        twoPlayersViewController.wordsText = self.wordsText;*/
        twoPlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        break;
    case 3:
        ThreePlayersViewController *threePlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ThreePlayersViewController"];
        /*threePlayersViewController.dictionary = self.words;
        threePlayersViewController.wordsText = self.wordsText;*/
        threePlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:threePlayersViewController animated:YES completion:nil];
        break;

    case 4:
        FourPlayersViewController *fourPlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FourPlayersViewController"];
        /*fourPlayersViewController.dictionary = self.words;
        fourPlayersViewController.wordsText = self.wordsText;*/
        fourPlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:fourPlayersViewController animated:YES completion:nil];
        break;
}

HOWEVER ifelse ifelse代码运行正常,但switch语句在运行编译器时会产生很多错误。最重要的是"预期的表达"在实例化视图控制器的行上给出。我猜它正在期待一个"表达"对于case blah:并且该行不足以作为"表达式"出于某种奇怪的原因。因为它似乎没有注册该行并给视图控制器一个标识符,所以它提供了更多的错误,主要包括"使用未声明的标识符' blahPlayerViewController&#39 ;;你的意思是' BlahViewController'?"。 为什么会这样?我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

关于" switch"的一个不幸的事情;陈述是you can't declare variables within them

您需要在switch语句的上方和外部声明变量。

类似的东西:

OnePlayerViewController *onePlayerViewController;
TwoPlayersViewController *twoPlayersViewController;

switch (self.players) {
    case 1:
        onePlayerViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OnePlayerViewController"];
        onePlayerViewController.dictionary = self.words;
        onePlayerViewController.wordsText = self.wordsText;
        onePlayerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:onePlayerViewController animated:YES completion:nil];
        break;
    case 2:
        twoPlayersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"TwoPlayersViewController"];
        twoPlayersViewController.dictionary = self.words;
        twoPlayersViewController.wordsText = self.wordsText;
        twoPlayersViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        break;