关闭发件人视图控制器后执行segue [英] Performing segue after sender view controller is dismissed
问题描述
基本上,我在滑出菜单中有一个按钮(它是它自己的视图控制器,覆盖了Origin
屏幕的一部分,我们称它为Menu
),当按下该按钮时,它会与另一个菜单执行模态搜索控制器,例如Destination
.
Basically, I have a button in a slide-out menu (which is its own view controller that covers part of the Origin
screen, let's call it Menu
) that, when pressed, performs a modal segue to another controller, let's say Destination
.
是否可以通过按下Menu
中的按钮(转到Destination
),将Menu
退回到Origin
,然后选择Destination
?
Is there any way that upon pressing the button in Menu
(to go to Destination
), that I can dismiss Menu
back to Origin
, and THEN segue to Destination
?
听起来很傻,但这是我以前见过的应用程序所做的事情.在我的情况下,要这样做的原因是,当我在Destination
上按完成"时,它将控制器退回到Menu
,而当我希望将其退回到Origin
时.我不能只是从Destination
回到Origin
进行搜索.
It sounds silly but it's something that I think I've seen apps do before. In my case, the reason for wanting to do this is that once I press "Done" on Destination
, it dismisses that controller back to Menu
, when I want it to just dismiss back to Origin
. I can't just perform a segue back to Origin
from Destination
.
代码:
这是我从Origin
打开Menu
的方式:
let interactor = Interactor()
@IBAction func openMenu(_ sender: AnyObject) {
performSegue(withIdentifier: "openMenu", sender: nil)
}
@IBAction func edgePanGesture(sender: UIScreenEdgePanGestureRecognizer) {
let translation = sender.translation(in: view)
let progress = MenuHelper.calculateProgress(translationInView: translation, viewBounds: view.bounds, direction: .Right)
MenuHelper.mapGestureStateToInteractor(
gestureState: sender.state,
progress: progress,
interactor: interactor){
self.performSegue(withIdentifier: "openMenu", sender: nil)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? MenuViewController {
destinationViewController.transitioningDelegate = self
destinationViewController.interactor = interactor
destinationViewController.currentRoomID = self.currentRoomID
}
}
这是我目前从Menu
到Destination
的prepareForSegue
,没什么好看的:
This is my prepareForSegue
from Menu
to Destination
currently, nothing fancy:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
let inviteVC = segue.destination as! InviteVipViewController
inviteVC.currentRoomID = self.currentRoomID
}
最后解散Destination
只是一个简单的
And finally to dismiss Destination
is just a simple
@IBAction func cancelButtonPressed(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
我看到了这个问题,基本上是我要尝试的问题,但不幸的是没有答案:
I saw this question which is basically what I'm trying to do but there was no answer unfortunately: Performing segue after dismissing modal swift
抱歉,这听起来令人困惑,但是如果有人知道我在说什么,并且可以让我知道如何设置segues/prepareForSegue
使其起作用,那么任何输入都会受到赞赏!
Sorry if this sounds confusing, but if anyone knows what I'm talking about and can let me know how I can set up the segues/prepareForSegue
s to make it work, any input would be appreciated!
推荐答案
Based on a modification to this answer, the following should work:
在情节提要中,删除通过单击菜单按钮触发的segue,然后转到Destination.
In your storyboard, remove the segue that is triggered by tapping your menu button and goes to Destination.
创建一个新的序列,该序列从Origin视图控制器到Destination视图控制器.此任务将手动执行.
Create a new segue that goes from the Origin view controller to Destination view controller. This segue is going to be manually performed.
在菜单"中选择目标"选项后,让菜单自行关闭,然后在Origin上执行目标"序列,如下所示:
When your Destination option is selected in Menu, have Menu dismiss itself and then perform the Destination segue on Origin, like this:
// This code goes in Menu, and you should call it when
// the menu button is tapped.
//
// presentingViewController is Origin
weak var pvc = self.presentingViewController
self.dismiss(animated: true) {
// Menu has been dismissed, but before it is destroyed
// it calls performSegue on Origin
pvc?.performSegue(withIdentifier: "openDestination", sender: nil)
}
关闭目的地"后,您应该会看到起源",而根本不会看到菜单".
When Destination is dismissed, you should see Origin, without seeing Menu at all.
我在一个示例应用程序中对此进行了测试,其中菜单"不是滑出菜单,而是完整的模态视图控制器,并且对我有用.
I tested this in a sample app where "Menu" was not a slide out, but a full modal view controller, and it worked for me.
编辑:在使用@KingTim进行故障排除时,他发现我们需要将Segue从UINavigationController
(而不是Origin)连接到Destination.这是因为Origin在导航控制器内部.发现之后,它就起作用了.
While troubleshooting with @KingTim, he found that we needed to wire the segue from the UINavigationController
, not Origin, to the Destination. This is because Origin is inside a navigation controller. After that discovery, it worked.
这篇关于关闭发件人视图控制器后执行segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!