xcode 如何处理进入同一个视图控制器的两个 segue?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9609676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:33:33  来源:igfitidea点击:

How to handle two segues going to the same view controller?

xcodestoryboardsegue

提问by SpokaneDude

I have an iPhone app I'm currently building in XCode 4.3 with Storyboard. I have a "root" view controller with two (2) seques to a view controller containing a UIWebView. I want to be able to identify the segue (have already set unique identifiers for both segues) so I can "push" the correct content to the UIWebView, based on which segue was activated in the "root" view controller.

我有一个 iPhone 应用程序,我目前正在使用 Storyboard 在 XCode 4.3 中构建。我有一个“根”视图控制器,其中包含一个包含 UIWebView 的视图控制器的两 (2) 个序列。我希望能够识别转场(已经为两个转场设置了唯一标识符),以便我可以根据在“根”视图控制器中激活的转场将正确的内容“推送”到 UIWebView。

I think I have to use "prepareForSegue" method, but don't know where it would go. Where can I find out how to deal with two segues going to the same view controller? (I have google'd it and found nothing appropriate to my situation).

我想我必须使用“prepareForSegue”方法,但不知道它会去哪里。我在哪里可以找到如何处理进入同一个视图控制器的两个 segue?(我已经用谷歌搜索了它,但没有发现适合我的情况)。

Here is the code that I'm using:

这是我正在使用的代码:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"helpSegue"]) {
        NSLog(@"helpSegue");
    }
    else if ([segue.identifier isEqualToString:@"reportSegue"]) {
        NSLog(@"reportSegue");
    }
}

采纳答案by Caleb

You won't call -prepareForSegue:sender:, you'll implement it in your view controller. In it's override of that method, you can check the segue's identifierproperty (the segue is passed in as a parameter):

你不会调用-prepareForSegue:sender:,你会在你的视图控制器中实现它。在重写该方法时,您可以检查 segue 的identifier属性(segue 作为参数传入):

if ([segue.identifier isEqualToString:@"Segue Numero Uno"]) {
    // do something here
}

That lets you take some sort of action depending on which segue is causing the transition. You can set the identifier for each segue in the storyboard editor.

这让您可以根据导致转换的转场采取某种行动。您可以在故事板编辑器中为每个 segue 设置标识符。

Update:Based on the code you provided in your comment (which I've added to your question), you've got the right idea now. At this point, it's just a matter of good old fashioned debugging. Some things to check:

更新:根据您在评论中提供的代码(我已将其添加到您的问题中),您现在有了正确的想法。在这一点上,这只是一个很好的老式调试问题。要检查的一些事项:

  • Is the view controller in your storyboard (i.e. the one that the segues in question lead to) set up as an instance of the class which implements this -prepareForSegue:sender:? Check the controller's type in the storyboard.

  • Do the strings you use in your code exactlymatch the identifiers that you set for your segues in the storyboard? Capitalization, spelling, punctuation, and white space all count.

  • Is your -prepareForSegue:sender:method being called? Put a breakpoint there and debug. If it is being called, what is the identifier for the seguethat's passed in? If it's not being called, the view controller in the storyboard doesn't have the right class.

  • 您的故事板中的视图控制器(即有问题的 segue 导致的那个)是否设置为实现 this 的类的实例-prepareForSegue:sender:?在故事板中检查控制器的类型。

  • 您在代码中使用的字符串是否与您在故事板中为 segue 设置的标识符完全匹配?大写、拼写、标点符号和空格都算数。

  • 你的-prepareForSegue:sender:方法被调用了吗?在那里放置一个断点并进行调试。如果它被调用,segue传入的标识符是什么?如果它没有被调用,则故事板中的视图控制器没有正确的类。