【问题标题】:Can a button have a two segues to two different view controllers / how can i code a view controller change without a segue?一个按钮可以对两个不同的视图控制器有两个 segue / 我如何在没有 segue 的情况下编写视图控制器更改?
【发布时间】:2020-02-22 13:17:37
【问题描述】:

这是我现在拥有的,但它没有打开视图控制器。我不认为我的 let act1c = act1ViewController() 有效。

@IBAction func buttonPressed(_ sender: UIButton) {
    let act1vc = act1ViewController()

    let act2vc = activity2VC()

    switch sender.titleLabel?.text {
    case "cheer up":
     present(act1vc, animated: true, completion: nil)

    case "yay":
        present(act2vc, animated: true, completion: nil)

    default:
        break
    } 

【问题讨论】:

    标签: swift uiviewcontroller segue


    【解决方案1】:

    您可以从一个按钮获得 2 个 segue,但是您必须有条件地取消一个以覆盖 func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool,这将是一个令人困惑的设计。

    我建议从 ViewController 顶部的 viewController 图标连接你的两个 segue。在Attributes Inspector中给它们诸如"segueToAct1""segueToAct2"之类的标识符,然后用performSegue(withIdentifier:sender:)触发它们:

    @IBAction func buttonPressed(_ sender: UIButton) {
    
        switch sender.titleLabel?.text {
        case "cheer up":
            self.performSegue(withIdentifier: "segueToAct1", sender: nil)
    
        case "yay":
            self.performSegue(withIdentifier: "segueToAct2", sender: nil)
    
        default:
            break
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-25
      • 2018-01-04
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多