将数据从第一个VC传递到第三个(Swift)

时间:2017-10-26 08:53:39

标签: ios swift xcode swift3 uiviewcontroller

我有3个viewControllers,我以这种方式将数据从第一个传递到第二个(这是第一个VC)

 var steppers : UIStepper?

    @IBOutlet weak var hourLabel: UILabel!
    @IBOutlet weak var stepper: UIStepper!

    @IBAction func stepper(_ sender: UIStepper) {

      hourLabel.text = String(sender.value)
    }


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let nc = segue.destination as? SelectClass {
            nc.limit = Int(stepper.value)
        } }

在第二个VC我有var limit = 0。到这里一切正常,但我想要做的是将用户给步进器的值传递给第三个VC。我试图在第三个VC var limit2 = 0中创建另一个变量,并以这种方式将数据从secondo VC传递到第三个

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let vc = segue.destination as? ThirdClass {
            vc.limit2 = limit
        } }

但没有结果,我的var limit2 = 0总是有值0而不是我用步进器添加的值。我该怎么办?

这是第二次VC中的准备

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == nearbySearchSegueIdentifier {
            guard let category = sender as? QCategoryy else {
                return
            }
            if let selectedRows = tableView.indexPathsForSelectedRows {

                if let vc = segue.destination as? ThirdClass {
                vc.category = category
                vc.limit2 = limit
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

为什么不把它从第一到第二传递到第三?

FirstVC:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "tosecondsegue" {
          let itemController : Second = segue.destination as! Second
          itemController.limit = 0 //Whatever you want
    }
}

SecondVC:

var limit = 0

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       if segue.identifier == "tothirdsegue" {
            let itemController : ThirdVC = segue.destination as! ThirdVC
            itemController.limit2 = limit
       }
}

ThirdVC:

 var limit2 = 0

//Use it here now

答案 1 :(得分:0)

考虑到您当前的代码,可能 limit2 未设置,因为此条件无效:

// This condition could be failing sometimes
if let selectedRows = tableView.indexPathsForSelectedRows {
    if let vc = segue.destination as? ThirdClass {
        vc.category = category
        vc.limit2 = limit
    }
}

根据DocumentationindexPathsForSelectedRows会返回一个可选项,并且在您的segue中,如果该值不是nil,则只设置 limit2

尝试在任何条件之外设置 limit2 ,因此它将始终执行,如果您需要满足条件才能执行segue,检查shouldPerformSegue(withIdentifier:sender:) here

此外,如果VC3上需要的值与离开VC1时的值相同,则可以使用不同的方法在它们之间传递数据,而不必依赖VC2。

可以在VC1中创建静态变量并在推送到VC​​2之前设置 limit 值:

var steppers : UIStepper?

    @IBOutlet weak var hourLabel: UILabel!
    @IBOutlet weak var stepper: UIStepper!

    // ADD THIS
    static var globalLimit: Int = 0

    @IBAction func stepper(_ sender: UIStepper) {
      hourLabel.text = String(sender.value)
    }


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let nc = segue.destination as? SelectClass {
            nc.limit = Int(stepper.value)
            // AND THIS
            VC1ClassName.globalLimit = Int(stepper.value)
        } 
}

然后在VC3内加载您的全局值(可能是viewDidLoad):

override func viewDidLoad() {
    super.viewDidLoad()
    limit2 = VC1ClassName.globalLimit
}

您还可以创建一个StepperManager类或类似的负责管理数据的东西,而不是让数据通过ViewControllers,您可以随时从您需要的每个类中访问它