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.
This one works fine:
- (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;
}
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?
Thanks!
Edit:
The problem was fixed by all the answers given, and many thanks for all!
Here is my new code:
-(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;
}
}
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!
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!