[英](Swift) Unwind segue when multiple view controllers lead to same view?
I am trying to code a hangman game and am having trouble with unwind segues. 我正在尝试编写一个刽子手游戏,并且在解开细分时遇到麻烦。 I have multiple view controllers that all ultimately lead to the same view, where the user plays the actual hangman.
我有多个视图控制器,最终导致相同的视图,用户在其中播放实际的刽子手。 However, depending on the presenting controller, I want the game to be in different "modes" (ie: multiplayer, single player, etc.).
但是,根据呈现控制器,我希望游戏处于不同的“模式”(即:多人游戏,单人游戏等)。 I am trying to add a play again button that unwinds to the previous view controller, but am unsure how to unwind when there are multiple paths the user could have taken to get to this view.
我正在尝试添加一个可以解除前一个视图控制器的播放按钮,但是当用户可以使用多个路径进入此视图时,我不确定如何放松。
In other words, my app kind of goes: 换句话说,我的应用程序有点:
A -> B -> C or A - > B - > C或
A -> D -> C where C can (ideally) unwind to D or B. A - > D - > C其中C可以(理想地)展开到D或B.
I was wondering what the best way to implement this is? 我想知道实现这个的最佳方法是什么? Should I just embed all my view controllers in navigation controllers?
我应该只将所有视图控制器嵌入导航控制器吗? Or is there a way to present a certain view controller based on a certain condition?
或者有没有办法根据某种条件呈现某个视图控制器? Thank you for any help!
感谢您的任何帮助!
If you have any custom logic and want to invoke the unwind segue programmatically to different view controllers, here is how: 如果您有任何自定义逻辑并希望以编程方式将unwind segue调用到不同的视图控制器,请按以下步骤操作:
Add unwindFromCViewController
to both BViewController
and DBViewController
: 将
unwindFromCViewController
添加到BViewController
和DBViewController
:
BViewController.swift BViewController.swift
class BViewController : UIViewController { @IBAction func unwindFromCViewController(segue:UIStoryboardSegue) { } }
DViewController.swift DViewController.swift
class DViewController : UIViewController { @IBAction func unwindFromCViewController(segue:UIStoryboardSegue) { } }
In your storyboard, create this segue and give it a identifier, and link it to the action you defined above unwindFromCViewController
: 在您的故事板中,创建此segue并为其指定标识符,并将其链接到您在
unwindFromCViewController
上面定义的操作:
Invoke the unwind segue from code: 从代码中调用unwind segue:
self.performSegueWithIdentifier("unwindFromCViewControllerSugueId", sender: self)
With that, you can unwind to a previous view regardless where it is coming from. 有了它,您可以放松到以前的视图,无论它来自哪里。
The unwind segue process will generally determine the previous UIViewController instance automatically. 展开segue进程通常会自动确定先前的UIViewController实例。 The exact process is described in this Tech Note from Apple, but in summary:
Apple的技术说明中描述了确切的过程,但总结如下:
Starting from the view controller that initiated the unwind segue the search order is as follows:
从启动展开segue的视图控制器开始,搜索顺序如下:
The next view controller in the responder chain is sent a
viewControllerForUnwindSegueAction:fromViewController:withSender:
message.响应器链中的下一个视图控制器发送一个
viewControllerForUnwindSegueAction:fromViewController:withSender:
message。 For a view controller presented modally, this will be the view controller that calledpresentViewController:animated:completion:
.对于以模态方式呈现的视图控制器,这将是调用
presentViewController:animated:completion:
的视图控制器presentViewController:animated:completion:
. Otherwise, the parentViewController.否则,parentViewController。
The default implementation searches the receiver's
childViewControllers
array for a view controller that wants to handle the unwind action.默认实现在接收者的
childViewControllers
数组中搜索想要处理展开操作的视图控制器。 If none of the receiver's child view controllers want to handle the unwind action, the receiver checks whether it wants to handle the unwind action and returns self if it does.如果接收者的子视图控制器都不想处理展开动作,则接收器检查是否要处理展开动作并返回self(如果是)。 In both cases, the
canPerformUnwindSegueAction:fromViewController:withSender:
method is used to determine if a given view controller wants to handle the unwind action.在这两种情况下,
canPerformUnwindSegueAction:fromViewController:withSender:
方法用于确定给定视图控制器是否要处理展开操作。If no view controller is returned from
viewControllerForUnwindSegueAction:fromViewController:withSender:
in step one, the search repeats from the next view controller in the responder chain.如果没有从
viewControllerForUnwindSegueAction:fromViewController:withSender:
返回视图控制器viewControllerForUnwindSegueAction:fromViewController:withSender:
在第一步中,搜索从响应器链中的下一个视图控制器重复。
So, the precise process will depend on how you presented view controller C - for example, via a modal presentation segue or a push segue on a UINavigationController
but as long as both B and D implement the unwind action you should be good. 因此,精确的过程将取决于您如何呈现视图控制器C - 例如,通过
UINavigationController
上的模态演示segue或push segue,但只要B和D都实现了展开动作,您应该是好的。
我只是简单地调用代码popViewController:animated
如果你把它推到导航堆栈dismissViewController:animated
控制器,或者dismissViewController:animated
如果是模态呈现的dismissViewController:animated
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.