只有在按下按钮时才会以编程方式调用 Segue

问题描述 投票:0回答:0

如果满足特定条件或通过按下按钮,我正在尝试调用 segue。当程序加载时 if vehicles.count == 0 它会打印(“going to new vehicle”)但不执行 segue。但是,如果我按下按钮,它就会起作用。我努力了 "shouldPerformSegue(withIdentifier: "goToNewVehicle", sender: nil) 和 performSegue(withIdentifier:“goToNewVehicle”,发件人:self)“ 和

"self.selectVehicle(self)"

但仅此而已。这是代码。显然我对这一切都是陌生的。任何帮助表示赞赏。谢谢

override func viewDidLoad() {
    super.viewDidLoad()
    
    checkForAndLoadVehicles()
    
}


//MARK: Segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationVC = segue.destination as! NewVehicleController
}

@IBAction func selectVehicle(_ sender: Any) {
    print("button pressed")
    performSegue(withIdentifier: "goToNewVehicle", sender: self)

}

//MARK: Check for and Load vehicles
func checkForAndLoadVehicles() {
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
        return
    }
    let managedContext = appDelegate.persistentContainer.viewContext
    let vehicleFetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Vehicle")
    do {
        vehicles = try managedContext.fetch(vehicleFetchRequest)
        print(vehicles.count)
    } catch let error as NSError {
        print("Could not fetch. \(error), \(error.userInfo)")
    }
    if vehicles.count == 0 {
        print("going to new vehicle")
        performSegue(withIdentifier: "goToNewVehicle", sender: self)
        
        self.selectVehicle(self)


    } 

我已经尝试了显示的两个选项来调用 segue,但都不起作用

segue
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.