PrepareForSegue之谜

编程入门 行业动态 更新时间:2024-10-28 00:28:08
本文介绍了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之谜

本文发布于:2023-11-08 18:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570140.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:之谜   PrepareForSegue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!