gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:26:55 24 4
gpt4 key购买 nike

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

enter image description here

Click from MainViewController > present modally > ProfileViewController Then ProfileViewController > AboutViewController (dismissed)

在我的 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)
}

最佳答案

你可以做到这一点:

  • 使用 Storyboard。

只需按住 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)
}

关于ios - 单击模态 ViewController 上的按钮,然后转到新页面 swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52439303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com