使用storyboard segue iOS将数据传递给视图控制器 [英] pass data to view controller using storyboard segue iOS
问题描述
我知道有很多关于此事的帖子,但我已经尝试了一切,但没有任何效果。所以我试图将两个视图控制器之间的对象传递给嵌入在导航项中的DBKIngredientsViewController。我有一个带有标识符showIngredientsSegue的push segue到DBKIngredientsViewController。我收到的错误消息是:
I know there are lots of postings about this but I have tried everything and nothing has worked. So I have tried to pass an object between two view controllers, to a DBKIngredientsViewController embedded in a navigation item. I have a push segue with the identifier "showIngredientsSegue" to the DBKIngredientsViewController. The error message I receive is:
'NSInvalidArgumentException',原因:' - [DBKIngredientsViewController topViewController]:无法识别的选择器发送到实例0x8a92450'
'NSInvalidArgumentException', reason: '-[DBKIngredientsViewController topViewController]: unrecognized selector sent to instance 0x8a92450'
我正在使用的视图控制器嵌入在导航控制器中,我认为它正在弄乱它。这是怎么回事?为了清楚起见,DBKViewController已经嵌入在导航控制器中,push segue推送DBKViewController,而不是嵌入它的导航控制器。我尝试过不同的方法,但似乎都没有。
The view controller to which I am segueing is embedded in a navigation controller, which I think is messing it up. What's the way around this? To be clear, the DBKViewController is already embedded in a navigation controller, and the push segue pushes the DBKViewController, not the navigation controller embedding it. I have tried it different ways but none seem to work.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showIngredientsSegue"]){
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
DBKIngredientsViewController *controller = (DBKIngredientsViewController *)navController.topViewController;
controller.targetRecipe = selectedRecipe;
}
}
IMG http://i39.tinypic.com/11joea1.png
推荐答案
你确定 segue.destinationViewController
是一个 UINavigationController
?它似乎只是一个 DBKIngredientsViewController
所以这应该工作:
Are you sure that segue.destinationViewController
is a UINavigationController
? It seems like it's just a DBKIngredientsViewController
so this should work:
DBKIngredientsViewController *controller = (DBKIngredientsViewController *)segue.destinationViewController
此外,如果 DBKViewController
已经有一个导航控制器,如果你按 DBKIngredientsViewController
,你不需要第二个。如果您以模态方式显示 DBKIngredientsViewController
,则只需要第二个。
Also if DBKViewController
already has a navigation controller then you do not need a second one if you are pushing DBKIngredientsViewController
. You would only need a second one if you are modally displaying DBKIngredientsViewController
.
这篇关于使用storyboard segue iOS将数据传递给视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!