[英]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.