访问其他ViewControllers中的变量Swift 3

时间:2017-05-03 13:53:34

标签: ios swift variables viewcontroller

我有一个问题,我似乎无法解决自己,我有两个视图控制器,第一个包含三个存储整数的变量。在我的第二个视图控制器上,我有3个滑块,用一个数字操纵每个滑块下的标签。

当我单击第二个视图控制器上的按钮时,我希望这些3个滑块中的数字替换我的第一个视图控制器上的三个变量中设置的数字但是当我输入变量名称时它不会&# 39; t出现在第二个视图控制器中?

有人可以解释我可能做错了什么,因为我认为变量是公开的,并且可以在我的应用程序中全局访问,但我正在努力弄清楚我做错了什么。

非常感谢我提前

以下是我的一些代码:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var timer = Timer()

    var softTime = 180
    var mediumTime = 300
    var hardTime = 600

    var chosenTime = 0

我想要softTime,mediumTime& hardTime要从下一个视图控制器中的按钮更改:

import UIKit

class SettingsViewController: UIViewController {


    @IBOutlet weak var softLabel: UILabel!
    @IBOutlet weak var softSliderValue: UISlider!
    @IBAction func softSlider(_ sender: Any) {

        let currentValue = Int(softSliderValue.value)

        softLabel.text = "\(currentValue)"

    }

    @IBOutlet weak var mediumLabel: UILabel!
    @IBOutlet weak var mediumSliderValue: UISlider!
    @IBAction func mediumSlider(_ sender: Any) {

        let currentValue = Int(mediumSliderValue.value)

        mediumLabel.text = "\(currentValue)"

    }

    @IBOutlet weak var hardLabel: UILabel!
    @IBOutlet weak var hardSliderValue: UISlider!
    @IBAction func hardSlider(_ sender: Any) {

        let currentValue = Int(hardSliderValue.value)

        hardLabel.text = "\(currentValue)"

    }

    @IBAction func setTimesButton(_ sender: Any) {



    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

}

1 个答案:

答案 0 :(得分:0)

//in Second VC
protocol PassDataDelegte: class {

    func your method(first: String, second: String, third: String)
}

weak var delegate: PassDataDelegte?

func youction button() {

delegate?.yourmethod(first, timeString: second, third: date)
}



// in First VC
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "DeadlineSegue" {
            let dvc = segue.destinationViewController as! YourSecondViewController
            dvc.delegate = self
        }
    }


    extension YourFirstViewController: PassDataDelegte {
    func sendDateTime((first: String, second: String, third: String) {
      print(first)
      print(second)
      print(third)
    }