代码之家  ›  专栏  ›  技术社区  ›  Wings

在swift 4中自动关闭视图控制器

  •  3
  • Wings  · 技术社区  · 8 年前

    假设我有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 回复  |  直到 8 年前
        1
  •  3
  •   tailor    8 年前

    首先解雇您的 b 查看控制器,然后显示 c 打开查看控制器 A. 。

    试试这个。

    let vc = ThirdViewController()
    let navVC = UINavigationController(rootViewController: vc)
    
    if let parent = self.presentingViewController{
        self.dismiss(animated: true){
             parent.present(navVC, animated: true)
        }
    }
    
        2
  •  3
  •   Bijender Singh Shekhawat    8 年前

    如果您使用 segue 然后使用' Unwind segue '

    为此,请在“视图控制器”中编写给定代码

    @IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {
    
          self.performSegue(withIdentifier: "YourSegueForC_VeiwController", sender: nil) 
    
    }
    

    在B\U viewcontroller中,按下按钮调用“Unwind segue”。然后firest B\u VC关闭,然后C\u VC出现在A\u VC上。

    注意:-任何View\u控制器上只存在一个View\u控制器。如果要先添加另一个视图控制器,请从该视图控制器中删除或取消另一个Viewcontroller。

        3
  •  3
  •   Wings    8 年前

    感谢您的帮助,最后我使用代理和协议完成了这项工作。我们需要在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
    
        4
  •  2
  •   SPatel mash    8 年前

    使用 代表团 模式EX:UIImagePickerViewControl

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