Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift 4中自动关闭视图控制器_Ios_Swift - Fatal编程技术网

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

Ios 在swift 4中自动关闭视图控制器,ios,swift,Ios,Swift,假设我有3个ViewControllers标记为“A”、“B”和“C”。现在,“A”是窗口的rootViewController,它以模式表示“B”。当一个按钮在“B”中被点击时,它应该立即显示C并自动关闭“B”。我该怎么做?我看到了StackOverflow中的所有示例,但它们都不起作用 @IBAction func proceedBtnTapped(_ sender: Any) { weak var pvc = self.presentingViewController le

假设我有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)
    })
}

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

首先关闭b查看控制器,然后在A上显示c查看控制器

试试这个

let vc = ThirdViewController()
let navVC = UINavigationController(rootViewController: vc)

if let parent = self.presentingViewController{
    self.dismiss(animated: true){
         parent.present(navVC, animated: true)
    }
}

如果使用
segue
,则使用“
展开segue

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

@IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {

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

}
在B_viewcontroller中,按下按钮调用“Unwind segue”。然后firest B_VC解散,然后C_VC出现在A_VC上


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

谢谢你的帮助,我终于用代理和协议完成了。我们需要在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

使用委托模式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 {

}

我使用这段代码,它显示红色屏幕,但我想展示我创建的UI,我该怎么做?它显示整个黑色屏幕,我想这是因为导航。有没有办法通过导航连接到它?如果你想让ThirdViewController与导航控制器一起出现,那么创建一个新的导航控制器,并将ThirdViewController设置为它的根视图控制器,并显示新的导航控制器而不是vc。让我们来看看。“它不工作”不是很有帮助。请与协议和学员分享您尝试的内容,并解释哪些部分不起作用。感谢您的帮助,但我找到了答案:)并且您必须学会如何使用“Unwind segue”,它在许多方面对您都有帮助。:)