解除视图控制器并显示另一个视图控制器 [英] Dismissing a View Controller and displaying another View Controller

查看:83
本文介绍了解除视图控制器并显示另一个视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我有三个ViewController-V1 V2和V3.
  • 在V1上单击按钮,将显示带有动画的ViewController V2FlipHorizo​​ntal.
  • 现在从V2中单击按钮,应该关闭ViewController回到V1并显示ViewController V3.

  • I've got three ViewControllers - V1 V2 and V3.
  • A button click on V1, ViewController V2 is displayed with animation FlipHorizontal.
  • Now on a button click from V2, ViewController should be dismissed back to V1 and display ViewController V3.

我尝试了这段代码来实现此功能.虽然没有用.请帮忙!

I tried this piece of code to achieve this functionality. It's not working though. Please help!

注意:V2和V3在同一个StoryBoard中(开始).V1位于不同的故事板(主)中.

Note : V2 and V3 are in same StoryBoard (Start). V1 is in different story board(main).

var VC3 : ViewController3?

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "start",bundle: nil)

 self.dismissViewControllerAnimated(false, completion: {
            self.VC3 = (mainStoryboard.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3)
            self.presentViewController(self.VC3!, animated: true, completion: nil)
        })

更新-我收到此警告尝试在视图控制器不在窗口层次结构中的ViewController2上显示ViewController3!

Update- I get this warning Attempt to present ViewController3 on ViewController2 whose view is not in the window hierarchy!

推荐答案

警告指出您正在ViewController2上显示ViewController3,但视图层次结构中不存在ViewController2!".这意味着ViewController2在显示ViewController3之前已被关闭.因此,无法在ViewController2上展示ViewController 3

The warning states that "You are presenting ViewController3 on ViewController2, But ViewController2 is not there in the view hierarchy!". This means ViewController2 has dismissed before ViewController3 would be presented. So it is not possible to present ViewController 3 on ViewController2

使用此示例代码:

import UIKit

class ViewController: UIViewController, presentViewControllerThree{

    @IBAction func displayViewControllerTwoOnClick(_ sender: AnyObject) {
        let currentStoryboard: UIStoryboard = UIStoryboard(name: "Start",bundle: nil)
        let  vc2: ViewController2 = currentStoryboard.instantiateViewController(withIdentifier: "viewcont2") as! ViewController2
        vc2.delegate = self
        self.navigationController?.present(vc2, animated: true, completion: nil)
    }

    func presentVC3(){
        let currentStoryboard: UIStoryboard = UIStoryboard(name: "Start",bundle: nil)
        let  vc3: ViewController3 = currentStoryboard.instantiateViewController(withIdentifier: "viewcont3") as! ViewController3

         self.navigationController?.present(vc3, animated: true, completion: nil)
    }

}

ViewController2

 import UIKit

protocol presentViewControllerThree {
    func presentVC3()
}

class ViewController2: UIViewController {

    var delegate: presentViewControllerThree?

    @IBAction func dismissViewControllerOnClick(_ sender: AnyObject) {

        self.dismiss(animated: true, completion: nil)
        delegate?.presentVC3()

    }

}

ViewController3

   import UIKit

class ViewController3: UIViewController {

    @IBAction func dismissViewControllerOnClick(_ sender: AnyObject) {

        self.dismiss(animated: true, completion: nil)
    }


}

Main.storyboard屏幕截图:

请检查我的GitHub链接以测试示例项目:

Please check my GitHub link to test sample project:

https://github.com/k-sathireddy/DismissPresentViewControllers

这篇关于解除视图控制器并显示另一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆