为啥segues不起作用?无法切换视图

Posted

技术标签:

【中文标题】为啥segues不起作用?无法切换视图【英文标题】:Why segues does not work? can not switch view为什么segues不起作用?无法切换视图 【发布时间】:2021-02-21 04:54:25 【问题描述】:

**不太清楚为什么按钮计算时视图没有改变 被按下? 可能的原因是什么?你能帮忙吗?

import UIKit
class CalculateViewController: UIViewController 
    @IBOutlet weak var heightLabel: UILabel!
    @IBOutlet weak var weightLabel: UILabel!
    @IBOutlet weak var heightSlider: UISlider!
    @IBOutlet weak var weightSlider: UISlider!
    
    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    

    @IBAction func heightSlided(_ sender: UISlider) 
        var currentHeightValue = sender.value
        let roundedHeightValue = String(format: "%.2f", currentHeightValue)
        heightLabel.text = "\(roundedHeightValue)m"
    
    
    @IBAction func weightSlided(_ sender: UISlider) 
        var currentWeightValue = sender.value
        let roundedWeightValue = String(format: "%.0f", currentWeightValue)
        weightLabel.text = "\(roundedWeightValue)kg"
    
    
    @IBAction func calculateBMI(_ sender: UIButton) 
        var squareOfHeight = Float(pow(heightSlider.value, 2.0))
        var BMI = weightSlider.value / squareOfHeight
        
        self.performSegue(withIdentifier: "goToResult", sender: self)
   
    
    

【问题讨论】:

你需要做一点调试... calculateBMI() 真的被调用了(可能它没有连接到右键)?如果它 is 被调用,请检查您的 Segue 的设置......也许是错字?例如您在 Storyboard 中输入了“gotoResult”而不是“goToResult”? 【参考方案1】:

试试这个..

让storyBoard: UIStoryboard = UIStoryboard(name: "Name 故事板”,捆绑:无) if let push = storyBoard.instantiateViewController(withIdentifier: "Name Storyboar ID") as? Name View Controller self.navigationController?.pushViewController(推,动画:真)

【讨论】:

以上是关于为啥segues不起作用?无法切换视图的主要内容,如果未能解决你的问题,请参考以下文章

Swift performSegueWithIdentifier 不起作用

在容器视图中导航时,Unwind Segue 不起作用

Unwind Segue 在 iOS 8 中不起作用

从 Unwind segue 触发时,Push segue 不起作用

Unwind segue 不起作用 SWIFT

IOS xcode 6 - Segues 不起作用