简体   繁体   中英

how to present and dismiss at same time

This is my Code

 @IBAction func backButton(_ sender: UIButton) {
   self.dismiss(animated: true, completion: { () -> Void in
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "RequestItemPackageDetails") as! RequestItemPackageDetails
        self.present(vc, animated: true, completion: nil)
                })
}

this is not working only dissmising

I dont't understand your questions well. I think you want to present a new ViewController. You can use this:

UIView.animate{duration: 0.5, animations: {
    (UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = NextViewController()
}}

You can dismiss self and then present another view from rootViewController

self.dismiss(animated: true, completion: { () -> Void in
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "RequestItemPackageDetails") as! RequestItemPackageDetails
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window!.rootViewController?.present(vc, animated: true, completion: nil)
})

Problem is that you are dismissing self and then trying to present a new view controller on that exact same self - but at that moment self is already dismissed. You need to present that new view controller from the parent of the current view controller. I believe the best way is to setup a delegate. So the controller that you showed us and that you want to dismiss will have this:

protocol FirstDelegate: class {
    func firstDismiss(_ first: First)
}

class First: UIViewController {
    weak var delegate: FirstDelegate?

    @IBAction func backButton(_ sender: UIButton) {
        // this will tell the delegate to dismiss this contorller and present the other one
        delegate?.firstDismiss(self)
    }

    // rest of the code omitted
}

Next, you will have to setup this in the parent that presents First view controller (here I assume that presentFirst method presents the First view controller):

class ParentViewController: UIViewController, FirstDelegate {

    // rest of the code

    func presentFirst() {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let firstVC = storyboard.instantiateViewController(withIdentifier: "First") as! RequestItemPackageDetails
        // set delegate to first:
        firstVC.delegate = self
        self.present(firstVC, animated: true, completion: nil)
    }

    func firstDismiss(_ first: First) {
        first.dismiss(animated: true, completion: { () -> Void in
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "RequestItemPackageDetails") as! RequestItemPackageDetails
            self.present(vc, animated: true, completion: nil)
        })
    }
}

Not quite sure with your Qns, but try this one out.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RequestItemPackageDetails") as! RequestItemPackageDetails
vc.previousVC = self 

self.present(vc, animated: true)

RequestItemPackageDetails.swift

var previousVC : UIViewController!

override viewDidLoad() {   
  previousVC.dismiss(animated: false, completion: nil)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM