本文介绍了PrepareForSegue之谜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个不同的VC中有一个prepareForSegue方法。一个使用 if 语句,而另一个使用开关。代码几乎完全相同,但名称除外。

I've got a prepareForSegue method in two different VCs. One uses an if statement, while the other is intended to use a switch. The code is virtually identical, except for names.

这个工作正常:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
        NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    if ([[segue identifier] isEqualToString:@"addActivity"])
    {
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        AddActivityViewController *aavc = (AddActivityViewController *)navController.topViewController;
        aavc.delegate = self;
        ListActivity *addedActivity = (ListActivity *)[ListActivity MR_createInContext:localContext];
        aavc.thisActivity = addedActivity;
    }

这个给了我两个警告。在第一行,我得到一个预期表达式警告。在第二行,我得到使用未声明的标识符'NavController'。

This one gives me two warnings. On the first line, I get an "Expected expression" warning. On the second line, I get "Use of undeclared identifier 'NavController'.

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    [SearchSpecs MR_truncateAllInContext:localContext];
    [localContext MR_saveToPersistentStoreAndWait];

    switch ([sender tag])
    {
        case aVsAButton_tag:
            UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
            AvsAViewController *aVSaVC = (AvsAViewController *)navController.topViewController;
            aVSaVC.delegate = self;
            SearchSpecs *thisSpec = (SearchSpecs *)[SearchSpecs MR_createInContext:localContext];
            aVSaVC.currentSpec = thisSpec;

            break;

        default:
            break;
    }

}

有人可以指出我的错误吗?

Can someone please point out my mistake?

谢谢!

修改:

问题由所有给出的答案修正,并且非常感谢所有人!

The problem was fixed by all the answers given, and many thanks for all!

这是我的新代码:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    [SearchSpecs MR_truncateAllInContext:localContext];
    [localContext MR_saveToPersistentStoreAndWait];

    switch ([sender tag])
    {
        case aVsAButton_tag:
        {
            UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
            AvsAViewController *aVSaVC = (AvsAViewController *)navController.topViewController;
            aVSaVC.delegate = self;
            SearchSpecs *thisSpec = (SearchSpecs *)[SearchSpecs MR_createInContext:localContext];
            aVSaVC.currentSpec = thisSpec;
        }
            break;

        default:
            break;
    }

}

当我添加分号时根据第三个答案的建议,我在默认值行收到了Switch case in protected scope的警告。但是,当我将 case 代码括在大括号中时,所有问题都会消失。我记得很好的事情!

When I added the semi-colon per the suggestion of the third answer, I got the warning that "Switch case is in protected scope" at the default: line. However, when I enclosed the case code in curly brackets, all problems evaporated. Very good thing for me to remember!

我会检查所有的答案,但由于它们都是同时到达的,所以如果我接受,我希望没有人会被冒犯最佳。为所有人+1,再次感谢!

I would green-check all answers, but since they all arrived about simultaneously, I hope no one will be offended if I accept the top one. +1 for all, and thanks again!

推荐答案

在C / Objective-C中,你不能在switch语句中声明变量那。如果要声明变量以用于switch语句的特定情况,可以将该案例的所有代码放在语句块中:

In C/Objective-C, you cannot declare variables in a switch statement like that. If you want to declare variables for use in a specific case of a switch statement, you can put all the code for that case in a statement block:

switch ([sender tag])
{
    case aVsAButton_tag:
    {
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        AvsAViewController *aVSaVC = (AvsAViewController *)navController.topViewController;
        aVSaVC.delegate = self;
        SearchSpecs *thisSpec = (SearchSpecs *)[SearchSpecs MR_createInContext:localContext];
        aVSaVC.currentSpec = thisSpec;
    }
        break;

    default:
        break;
}

这篇关于PrepareForSegue之谜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:28