从另一个视图控制器访问更新的变量值

时间:2016-03-18 18:29:37

标签: ios xcode swift

我有一个包含三个不同视图控制器的项目:

ViewController(这是用户输入值的位置),LoadingViewController(加载屏幕)和AnswerViewController(我希望加载第一个视图控制器中更新值的结果。)

以下是ViewController中的代码:

var calculatedRatio: Double?

@IBOutlet weak var boysCountTextField: UITextField!
@IBOutlet weak var girlsCountTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func calculateButtonTapped(sender: UIButton) {
    if boysCountTextField.text != "" && girlsCountTextField.text != "" {
        calculateRatio()
        self.performSegueWithIdentifier("loadingAnswer", sender: self)
    }
}

func calculateRatio() {

    let boysCount = Double(boysCountTextField.text!)!
    let girlsCount = Double(girlsCountTextField.text!)!

    let currentRatio = boysCount / girlsCount

    calculatedRatio = currentRatio
    print(calculatedRatio)

}

正如您所看到的,在此视图控制器中,我在顶部创建了一个名为calculatedRatio的变量,它是一个可选的双精度变量。在同一个视图控制器中,一旦按下按钮,它就会在底部运行一个名为calculateRatio()的函数,它会更新calculatedRatio值。

我的问题是,我怎么能在不同的视图控制器(AnswerViewController)中访问这个变量的新值,而我没有直接进入?

我已经尝试在AnswerViewController中创建一个ViewController实例,但是该变量的初始值(在本例中为nil)是复制的,而不是calculateRatio()函数给出的新值。 / p>

2 个答案:

答案 0 :(得分:0)

因此,您希望在处理复杂方法时显示加载屏幕,该加载屏幕是其自己的视图控制器。我假设你有理由想在导航堆栈上抛出加载屏幕,而不是做某种类型的模态叠加。我还假设有一个原因必须在第一个View控制器上进行计算(而不是加载屏幕),并且没有更好的方法可以将计算抽象出去。为什么不尝试NSNotifications?您可以侦听计算以在LoadingViewController中完成并将更新值作为对象传递。

答案 1 :(得分:0)

您可以使用iOS应用程序的委托模式。在这里,我试图解决你的问题。我做了一些更改你的Input ViewController类来实现AnswerViewController的委托。

class ViewController: UIViewController,AnswerVCProtocol {
  var calculatedRatio: Double?

  @IBOutlet weak var boysCountTextField: UITextField!
  @IBOutlet weak var girlsCountTextField: UITextField!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  @IBAction func calculateButtonTapped(sender: UIButton) {
    if boysCountTextField.text != "" && girlsCountTextField.text != "" {
      calculateRatio()
      self.performSegueWithIdentifier("loadingAnswer", sender: self)
    }
  }

  func calculateRatio() {

    let boysCount = Double(boysCountTextField.text!)!
    let girlsCount = Double(girlsCountTextField.text!)!

    let currentRatio = boysCount / girlsCount

    calculatedRatio = currentRatio
    print(calculatedRatio)

  }

  // MARK: AnswerViewController Delegate

  func getUpdateRadio()-> Double?{
    return self.calculatedRatio
  }

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    // Please check you identifier as you given in storyboard
    if segue.identifier == "loadingAnswer"{

      let ansVc = segue.destinationViewController as! AnswerViewController

      ansVc.delegate = self

    }

  }


}

这是协议

protocol AnswerVCProtocol{
  func getUpdateRadio()-> Double?
}

现在我猜你的AnswerViewController是这样的。这里我只展示如何通过委托访问变量。

class AnswerViewController: UIViewController {

  var delegate: AnswerVCProtocol?

  override func viewDidLoad() {
    <#code#>
  }
  // To receive update value call this function when you needed
  // Receive Update Value from your Input ViewController
  func updateRadio(){
    if let _ = self.delegate{
     let updateRatio =  delegate?.getUpdateRadio()
    }

  }

}

<强>概述

  1. 声明协议。在这种情况下AnswerVCProtocol

  2. 确认输入ViewController中的协议。