简体   繁体   English

在发送方视图控制器被解除后执行 segue

[英]Performing segue after sender view controller is dismissed

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 .基本上,我在滑出式菜单中有一个按钮(它是它自己的视图控制器,它覆盖了Origin屏幕的一部分,我们称之为Menu ),当按下时,它会执行到另一个控制器的模式切换,比如说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 ?有什么办法可以在按下Menu的按钮(转到Destination )后,我可以将Menu解散回Origin ,然后转到Destination

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 .就我而言,想要这样做的原因是,一旦我在Destination上按下“完成”,它就会将该控制器解散回Menu ,而我希望它只是解散回Origin I can't just perform a segue back to Origin from Destination .我不能只是执行从Destination返回Origin

Code:代码:

This is how I open the Menu from Origin :这就是我从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
    }
}

This is my prepareForSegue from Menu to Destination currently, nothing fancy:这是我目前从MenuDestination prepareForSegue ,没什么特别的:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)

    let inviteVC = segue.destination as! InviteVipViewController
    inviteVC.currentRoomID = self.currentRoomID
}

And finally to dismiss Destination is just a simple最后关闭Destination只是一个简单的

@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我看到了这个问题,这基本上就是我想要做的,但不幸的是没有答案: 在解雇 modal swift 后执行 segue

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!抱歉,如果这听起来令人困惑,但如果有人知道我在说什么并且可以让我知道如何设置 segues/ prepareForSegue使其工作,任何输入将不胜感激!

Based on a modification to this answer , the following should work:基于对此答案的修改,以下应该有效:

In your storyboard, remove the segue that is triggered by tapping your menu button and goes to Destination.在您的故事板中,删除通过点击菜单按钮触发的转场并转到目的地。

Create a new segue that goes from the Origin view controller to Destination view controller.创建一个从 Origin 视图控制器到 Destination 视图控制器的新 segue。 This segue is going to be manually performed.此转场将手动执行。

When your Destination option is selected in Menu, have Menu dismiss itself and then perform the Destination segue on Origin, like this:当您在 Menu 中选择 Destination 选项时,让 Menu 自行关闭,然后在 Origin 上执行 Destination segue,如下所示:

    // 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.当 Destination 被关闭时,您应该会看到 Origin,而根本不会看到 Menu。

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.我在一个示例应用程序中对此进行了测试,其中“菜单”不是滑出,而是一个完整的模态视图控制器,它对我有用。

EDIT: While troubleshooting with @KingTim, he found that we needed to wire the segue from the UINavigationController , not Origin, to the Destination.编辑:在使用@KingTim 进行故障排除时,他发现我们需要将转场从UINavigationController而非 Origin 连接到 Destination。 This is because Origin is inside a navigation controller.这是因为 Origin 位于导航控制器内。 After that discovery, it worked.在那个发现之后,它起作用了。

If your presenting view is embedded in a navigation controller then you can do this:如果您的呈现视图嵌入在导航控制器中,那么您可以执行以下操作:

    weak var pvc:UIViewController! = self.presentingViewController?.childViewControllers[0]
    dismiss(animated: true)
    {
        pvc.performSegue(withIdentifier: "SegueID", sender: nil)
    }

Simple solution with presentingViewController使用presentingViewController简单解决方案

if let destinationVC = self.presentingViewController as? YourViewController {
    destinationVC.isBooleanPassed = true
    destinationVC.selectedString = "here comes your string"
    destinationVC.selectedInteger = 12345
}

dismiss(animated: true, completion: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Xcode 中执行 segue 后如何关闭视图控制器? - How to close a View Controller after Performing segue in Xcode? 为什么我的视图控制器(从故事板中以模态方式呈现)在被解雇后不会被释放? - Why does my view controller (presented modally from a storyboard segue) not get released after being dismissed? 在目标视图 controller 中获取 segue 的发件人 - get sender of segue in destination view controller 在 Swift 中对另一个视图控制器执行转场后关闭游戏视图控制器 - Dismissing a Game View controller after Performing a Segue to another view controller in Swift 执行segue时快速杀死视图控制器 - Swift kill view controller when performing segue 解散模态视图控制器,然后执行segue - Dismissing a modal view controller and THEN performing a segue 快速嵌套在另一个视图控制器内部的视图控制器上执行segue - Performing a segue on a view controller that is nested inside of another view controller in swift 弹出“关闭”后刷新父视图上的IBOutlet外观 - Refresh IBOutlet appearance on parent view after popup segue is 'dismissed' 关闭UIPopoverController后呈现视图控制器 - Presenting a View Controller after a UIPopoverController is dismissed 编辑器视图控制器关闭后,UITableView不更新 - UITableView not updating after editor view controller dismissed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM