如何在同一时间呈现和解雇

时间:2018-01-26 07:32:57

标签: ios swift

这是我的代码

 @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)
                })
}

这不是唯一有用的瑕疵

4 个答案:

答案 0 :(得分:0)

我不太了解你的问题。我想你想要一个新的ViewController。你可以用这个:

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

答案 1 :(得分:0)

您可以解除self,然后从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)
})

答案 2 :(得分:0)

问题是您正在解雇self,然后尝试在完全相同的self上展示新的视图控制器 - 但此时self已被解雇。您需要从当前视图控制器的父级提供该新视图控制器。我认为最好的方法是设置一个代表。因此,您向我们展示并且您要解雇的控制器将具有以下内容:

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
}

接下来,您必须在显示First视图控制器的父级中设置此项(此处我假设presentFirst方法显示First视图控制器):

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)
        })
    }
}

答案 3 :(得分:0)

对你的Qns不太确定,但试试这个。

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)
}