在swift 4中自动关闭View Controller

时间:2018-03-12 11:29:38

标签: ios swift

我们说我有3 ViewControllers标有" A"," B"和" C"。现在," A"是窗口的rootViewController,它表示" B"模态。当按下按钮时," B"它应该立即以模态方式呈现C并且解雇" B"自动。我怎样才能做到这一点?我在StackOverflow中看到了所有示例,但它们无法正常工作

@IBAction func proceedBtnTapped(_ sender: Any) {
    weak var pvc = self.presentingViewController
    let vc = ThirdViewController()

    pvc?.present(vc, animated: true, completion: { [weak self] in
        self?.dismiss(animated: true, completion: nil)
    })
}

这是我的代码。有什么帮助吗?

4 个答案:

答案 0 :(得分:2)

首先关闭 b 视图控制器,然后在 A 上显示 c 视图控制器。

试试这个。

ComplexObject

答案 1 :(得分:2)

如果您使用segue,请使用' Unwind segue'

在您的' A视图控制器'

中写入给定代码
@IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {

      self.performSegue(withIdentifier: "YourSegueForC_VeiwController", sender: nil) 

}

在B_viewcontroller中,按下按钮调用'展开segue'。然后解雇B_VC,然后C_VC出现在A_VC上。

注意: - 任何View_controler上只有一个View_controler。如果要添加另一个,请从该View控制器中删除或关闭另一个ViewController。

答案 2 :(得分:2)

感谢您的帮助,最后我使用委托和协议完成了这项工作。我们需要在SecondViewController中创建协议,我们必须在FirstViewController中使用该协议。

// Declare protocol and variable delegate in SecondViewController
protocol dismissVC {
func presentVC()
}
var delegateVC: dismissVC? = nil

// Call that protocol in FirstViewController
  class FirstViewController: UIViewController, dismissVC {

        func presentVC() {
        let pickVc = UIStoryboard(name:"Main", bundle: 
        nil).instantiateViewController(withIdentifier: "third-VC") 
        as! ThirdViewController
        pickVc.modalPresentationStyle = .custom
        present(pickVc, animated: true, completion: nil)
        }

  }

  // Action method in SecondViewController
@IBAction func proceedBtnTapped(_ sender: Any) {
        self.dismiss(animated: true) {
        self.delegateVC!.presentVC()
    }

// and at last call that delegate when you try to use segue 

   let SecondVc = UIStoryboard(name:"Main", bundle: nil).instantiateViewController(withIdentifier: "second-VC") as! SecondViewController
                Vc.modalPresentationStyle = .custom
                Vc.delegateVC = self // add delegate here
             self.present(Vc, animated: true, completion: nil)
            })
 // At last call delegate
    yourVc.delegateVC = self

答案 3 :(得分:1)

使用委派模式EX:UIImagePickerViewControll

class A:UIViewController, BDelegate {

  @IBAction func onOpenBController(_ sender:Any) {
      let bVC = B()
      bVC.delegate = self
      present(bVC, animated: true, completion: nil)
  }

  func bControllerDidSelect(_ controller: B) {
    controller.dismiss(animated: true, completion: nil) //Dismiss B controller
    present(C(), animated: true, completion: nil) //Present C controller
  }
}

class B:UIViewController {
    weak var delegate:BDelegate?

    @IBAction func onSomeClickEvent(_ sender:Any) {
       delegate?.bControllerDidSelect(self)
    }
}

protocol BDelegate: class {
    func bControllerDidSelect(_ controller:B)
}

class C:UIViewController {

}
相关问题