我的全局变量正在从一种功能转移到另一种功能

时间:2019-04-19 14:18:56

标签: swift var

我有一个应用程序,当按下按钮时,应设置一个自定义变量,然后将其用于配置“ segue for segue”功能。基本上,在“准备功能”中,我正在设置一个switch语句来执行适当的代码。

但是,如果我在IBAction函数中为按钮设置了将由开关使用的var,则在执行switch命令之前var会丢失其值,而我得到的只是“ fail”的默认值。

我一直在寻找,我知道我在做一些非常愚蠢的事情,但是我是菜鸟,如果有的话,需要帮助!

import UIKit

var mainAppointment = Appt()

class SubMenuOneViewController: UIViewController {
    // func whatsTheService() -> ServiceType {
    //     let serviceSelected = ServiceType.appointment
    //      return serviceSelected
    //  }

    @IBAction func selectAppointmentSetting(_ sender: myFirstButton) {
        //let whatsSelected = serviceTypeSelected.airport

        mainAppointment.serviceType = .petvisit
        mainAppointment.petName = "Beans"
        performSegue(withIdentifier: "appointmentSettingSegue", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let nav = segue.destination as! UINavigationController
        let apptOrderVC = nav.topViewController as! AppointmentScreenTableViewController

        //switch (serviceTypeSelected).self {
        switch mainAppointment.petName {
        case "Beans":
            apptOrderVC.appointmentTypeString = "Beans"
        default:
            apptOrderVC.appointmentTypeString = "fail"
        }
    }

1 个答案:

答案 0 :(得分:0)

好吧,我尝试了在准备segue函数中使用segue标识符的另一种方法,并且效果很好。可能是更好的方法!感谢您的所有反馈!

   if segue.identifier == "appointmentSettingSegue" {
            apptOrderVC.appointmentTypeString = "Beans"
        } else {
            apptOrderVC.appointmentTypeString = "fail"