ios 了解 performSegueWithIdentifier

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9176215/
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-08-30 16:41:24  来源:igfitidea点击:

Understanding performSegueWithIdentifier

iosobjective-cxcodeuikitsegue

提问by Simon Barkhuizen

Can someone more knowledgeable than I explain performSegueWithIdentifier:sender:for me? I need to switch views (and classes) and also carry a few NSStrings and IDs over to that view's class. I was wondering if this is possible with performSegueWithIdentifier:sender:

有比我更了解的人可以performSegueWithIdentifier:sender:为我解释吗?我需要切换视图(和类),并将一些 NSString 和 ID 传递给该视图的类。我想知道这是否可行performSegueWithIdentifier:sender:

Thanks!

谢谢!

回答by jrturton

First, you have to have set up the segue in your storyboard and give it the appropriate identifier. (Click on the segue (left panel) and then click on Attributes (right panel).

首先,您必须在故事板中设置 segue并为其提供适当的 identifier。(单击 segue(左面板),然后单击 Attributes(右面板)。

You can then link this to buttons or selection of table rows from your storyboard, or you can call it in code using performSegueWithIdentifier:sender:.

然后,您可以将其链接到故事板中的按钮或表格行的选择,或者您可以使用performSegueWithIdentifier:sender:.

After this, your view controller will be sent the prepareForSegue:sender:message. You override this method in your view controller subclass, and can configure the target view controller as follows:

在此之后,您的视图控制器将收到prepareForSegue:sender:消息。您可以在视图控制器子类中覆盖此方法,并可以按如下方式配置目标视图控制器:

TargetViewController *targetVC = (TargetViewController*)segue.destinationViewController;
targetVC.string1 = string1;

And so forth. The senderin this method will be the object that you use as the senderin the original method call.

等等。在sender这一方法将是您作为使用对象sender在原来的方法调用。

回答by retainCount

Most segues are initiated automatically as the result of some user interaction. For instance, if you have a segue that is wired up from a button to a scene in a storyboard, when the button is tapped the segue will automatically initiate.

大多数 segue 是由于某些用户交互而自动启动的。例如,如果您有一个从按钮连接到情节提要中的场景的转场,当点击该按钮时,转场将自动启动。

Occasionally, it makes sense to trigger a segue programmatically - e.g. you have a High Scores scene that is displayed when the user wins a round of a game. There's no way to express the concept of winning in the storyboard itself, so you can instead create a segue, assign an identifier to it, and invoke -performSegueWithIdentifier:sender:at runtime.

有时,以编程方式触发 segue 是有意义的 - 例如,您有一个高分场景,当用户赢得一轮游戏时会显示该场景。没有办法在故事板本身中表达获胜的概念,因此您可以创建一个 segue,为其分配一个标识符,并-performSegueWithIdentifier:sender:在运行时调用。

The other segue related method on UIViewController, -prepareForSegue:sender:, is the method you should override to perform any customization on the destination view controller.

UIViewController 上的另一个与 segue 相关的方法-prepareForSegue:sender:是您应该重写以在目标视图控制器上执行任何自定义的方法。

回答by retainCount

In prepareForSegue:sender:you get a chance to configure the destinationViewController: that's where you'd pass it the data it needs. It's discussed in Cocoa Application Competencies for iOS.

prepareForSegue:sender:您有机会配置destinationViewController: 那就是您将它需要的数据传递给它的地方。Cocoa Application Competencies for iOS 中对此进行了讨论。

回答by Jeroen Leenarts

Today I ran into the issue of performSegueWithIdentifier: not executing due to the fact of not having set a delegate queue on my URL session.

今天我遇到了 performSegueWithIdentifier 的问题:由于没有在我的 URL 会话上设置委托队列,所以没有执行。

So by any chance, check if you are actually setting a delegate queue when creating your URLSession, else URLSession will create it's own.

因此,无论如何,请检查您在创建 URLSession 时是否实际设置了委托队列,否则 URLSession 将创建它自己的队列。

urlSession = [NSURLSession sessionWithConfiguration:sessionConfigObject
                                           delegate:self
                                      delegateQueue:[NSOperationQueue mainQueue]];

I mention this here because I quite often see URLSession handling ending up calling some sort of UI related activity. And performSegue needs to be executed on main, or else it will do just nothing.

我在这里提到这一点是因为我经常看到 URLSession 处理最终调用了某种 UI 相关的活动。而 performSegue 需要在 main 上执行,否则它什么也不做。