繁体   English   中英

单击模态ViewController上的按钮,然后转到新页面swift 4

[英]click button on modal ViewController then go to new page swift 4

我的情况是我想在单击按钮时从ViewController模态显示新页面,我们可以像android一样仅在按钮上onClick并更改页面吗?

在此处输入图片说明

从MainViewController单击>模态呈现> ProfileViewController,然后单击ProfileViewController> AboutViewController(关闭)

在我的ProfileViewController

@IBAction func testSupport(_ sender: Any) {
    let aboutView = self.storyboard?.instantiateViewController(withIdentifier: "AboutViewController") as! AboutViewController
    self.navigationController?.pushViewController(aboutView, animated: true)
    self.dismiss(animated: false, completion: nil)
}

您可以实现:

  • 使用情节提要。

只需将Ctrl从ViewController拖动到所需的AboutViewController。 然后将出现一个弹出窗口,您可以在其中选择要执行的序列(模态)

现在单击segue(在2个ViewController之间创建的链接,并为其指定一个标识符)。

将此代码添加到您的ViewController(您当前所在的代码)中

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "yourSegueId" {
         let aboutViewController = segue.destination as! AboutViewController
         // do some init if you want
    }
}

在您的按钮内执行segue操作:

@IBAction func didTapButton(_ sender: Any) {

     self.performSegue(withIdentifier: "yourSegueId", sender: self)
}
  • 编程。

您可以像当前一样显示ViewController,但是您需要模态显示,因此:

 @IBAction func didTapButton(_ sender: Any) {

     let mainStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
     let aboutViewController = mainStoryboard.instantiateViewController(withIdentifier: "AboutViewController") as! AboutViewController

     self.present(aboutViewController, animated: true, completion: nil) nil)
}

您可以为此使用委托。 在您的ProfileViewController.swift声明一个协议

protocol ProfileViewControllerDelegate {
    func didPressSupportButton()
}

然后在您的ProfileViewController类中声明

var delegate: ProfileViewControllerDelegate?

在您的支持行动中:

@IBAction func testSupport(_ sender: Any) {
    delegate?.didPressSupportButton()
    self.dismiss(animated: false, completion: nil)
}

呈现视图时,如果您是通过编程方式进行的,或者是在准备进行搜索时:

yourModalViewController.delegate = self

然后在您的MainViewController遵守协议

extension MainViewController: ProfileViewControllerDelegate {
    func didPressSupportButton {
        let aboutView = self.storyboard?.instantiateViewController(withIdentifier: "AboutViewController") as! AboutViewController
        self.navigationController?.pushViewController(aboutView, animated: true)
     }    
}

呈现您的“自我”。 它没有navigationcontroller,所以您可以pushviewcontroller。 尝试:

@IBAction func testSupport(_ sender: Any) {
    let aboutView = self.storyboard?.instantiateViewController(withIdentifier: "AboutViewController") as! AboutViewController
    self.dismiss(animated: true) {
        if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController {
            topController.navigationController?.pushViewController(aboutView, animated: true)
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM