在使用Swift展示新的Viewcontroller后如何关闭以前的Viewcontroller?

时间:2019-05-15 06:29:40

标签: ios swift xcode uiviewcontroller

我的场景,在我的项目中,我维护三个ViewController (Main, VC1 and VC2)。在主ViewController中,我维护着UIButton,此按钮单击以VC1呈现模型视图控制器。再次,VC1我保持UIButton,并单击操作以将模型呈现给VC2VC2演示后,我需要关闭VC1。

//  presenting ViewController
var presentingViewController: UIViewController! = self.presentingViewController

self.dismissViewControllerAnimated(false) {
      // go back to MainMenuView as the eyes of the user
      presentingViewController.dismissViewControllerAnimated(false, completion: nil)
}

6 个答案:

答案 0 :(得分:2)

尝试VC2的关闭按钮操作

var vc = self.navigationController?.presentingViewController
while vc?.presentingViewController != nil {
    vc = vc?.presentingViewController
}
vc?.dismiss(animated: true, completion: nil)

答案 1 :(得分:1)

您需要从mainVC中打开vc2-为此,您需要一个委托方法,该方法将告诉mainVC关闭当前打开的vc(即vc1),并在成功块中打开vc2。

代码片段:-

dismiss(动画化:false){    //打开vc2    存在(vc2) }

答案 2 :(得分:0)

希望这会起作用:

您需要有一个UIViewController作为基础,在这种情况下,iv_play.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { iv_play.setSelected(!iv_play.isSelected()); if (iv_play.isSelected()) { ((GifDrawable) gif_1.getDrawable()).start(); ((GifDrawable) gif_2.getDrawable()).start(); } else { iv_play.setSelected(false); ((GifDrawable) gif_1.getDrawable()).stop(); ((GifDrawable) gif_2.getDrawable()).stop(); } } }); 是基础ViewController。您需要使用协议来调用Controller之间的导航。

您可以使用协议:-

进入您的FirstViewController设置协议:

MainViewController

现在在您的MainViewController中

protocol FirstViewControllerProtocol {
    func dismissViewController()
}

class FirstViewController: UIViewController {

    var delegate:FirstViewControllerProtocol!

    override func viewDidLoad() {
        super.viewDidLoad()

        }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    @IBAction func goBack(sender: AnyObject) {
        self.dismissViewControllerAnimated(true) { 
            self.delegate!.dismissViewController()
        }
    }

答案 3 :(得分:0)

在这种情况下,您需要从显示其他视图控制器的视图控制器(在您的情况下为Main)中调用dismiss。

正如您在上面的问题中陈述的情况一样,Main提供VC1,VC1提供VC2: 然后调用Main.dismiss(动画:true,完成:nil)将同时关闭VC1和VC2。

如果您没有对根控制器(Main)的引用,则可以链接几个presentingViewController属性以进行访问;在最顶层的控制器(VC2)中是这样的:

@Transactional(rollbackFor=Exception.class, propagation=Propagation.REQUIRES_NEW)
private void test1(Account account) throws Exception {
    DOA.save(account);
    status = test2(account);
    if(status!='SUCCESS'){
        throw new Exception("api call failed");
    }
}
@Transactional(propagation=Propagation.MANDATORY)
private void test2(Account account) {
    response //API Call
    DOA.save(response);
    return response.status;
}

我希望这会有所帮助。

答案 4 :(得分:0)

有一个present(_:animated:completion:)方法需要完成。在其中可以dismiss VC1。

您的代码将如下所示

@IBAction func ButtonTapped(_ sender: Any) {
    present(VC2, animated: true) {
        dismiss(animated: true, completion: nil)
    }
}

答案 5 :(得分:0)

要解决问题陈述,您可以使用VC2而不是MainVC1进行操作。

我们可以使用

Main中引用VC1
self.presentingViewController

显示VC2时,请用VC1方法的completionHandler关闭present(_:animated:completion:)

class Main: UIViewController {
    @IBAction func onTapButton(_ sender: UIButton) {
        let vc1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "VC1")
        self.present(vc1, animated: true, completion: nil)
    }
}

class VC1: UIViewController {
    @IBAction func onTapButton(_ sender: UIButton) {
        let vc2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "VC2")
        vc2.view.backgroundColor = .blue
        self.dismiss(animated: false, completion: nil)
        self.presentingViewController?.present(vc2, animated: true, completion: nil)
    }
}

class VC2: UIViewController {

}

此方法可提供预期的输出。如果需要其他任何条件,请允许我。