在不同类的值更改之前和之后访问相同的变量 - Swift

时间:2015-09-29 21:45:48

标签: ios xcode swift variables

我一直坚持一个简单的概念(我猜),基本上我在Storyboard上有两个ViewControllers,我有两个类,ViewController和ViewController2:

我有一个标签,一个默认值(0),当我点击按钮我想将此变量的值更改为10,然后我点击“显示”按钮,我打印这个变量,我我成功地改变了标签并打印了新的价值。

真正的问题是,当我想从另一个视图中获取新的变量值时,即使我更改了值,如果我尝试在第二个视图上打印变量,变量总是返回默认值(0)

enter image description here

的ViewController

import UIKit

class ViewController: UIViewController {

var variable = "0"
@IBOutlet var defaultLabel: UILabel!
@IBOutlet var label1Label: UILabel!

@IBAction func setValue(sender: AnyObject) {
    setValue()
}


@IBAction func getValue(sender: AnyObject) {
    getValue()
}
override func viewDidLoad() {
    super.viewDidLoad()
}

func setValue(){
    variable = "10"
    defaultLabel.text = variable
}

func getValue(){
    print(variable)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


}

ViewController2

import UIKit

class ViewController2: UIViewController {

@IBOutlet var label2Label: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func show(sender: AnyObject) {
 print(ViewController().getValue())

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
}

我发现这篇文章:

Access variable in different class - Swift

我真的认为这是我找到解决方案的方式,但我真的不明白如何在ViewController2上调用变量。

感谢。

2 个答案:

答案 0 :(得分:0)

使用代表!

以下是ViewController1 ViewController2代表的示例:

  1. 定义协议:

    protocol VariableManager {
      func getValue() -> Int
    }
    
  2. 然后,在ViewController1中,修改getValue方法,以使ViewController1符合协议:

    class ViewController1: VariableManager {
    
      func getValue() -> String {
       return variable
      }
    
    }
    
  3. 现在在名为ViewController2的{​​{1}}中定义一个变量:

    delegate
  4. class ViewController2 { var delegate: VariableManager? } 中的prepareForSegue方法:

    ViewController1
  5. 现在在override func prepareForSegue(segue: UIStoryboardSegue) { if let identifier = segue.identifier { switch identifier { case "MySegueIdentifier": let destination = segue.destinationViewController as! 'ViewController2' destination.delegate = self default: break } } } 中,更改ViewController2方法:

    show
  6. 代表团是一种非常普遍且非常重要的模式。我建议您仔细阅读:https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html

    尝试在@IBAction func show(sender: AnyObject) { if let delegate = delegate { let variable = delegate.getValue() print(variable) } 内实例化ViewController1的另一个瞬间不是良好做法。

答案 1 :(得分:0)

@IBAction func show(sender: AnyObject) { 
    print(ViewController().getValue())
}

ViewController() - 这是类构造函数,每次调用 ViewController()时,它都会返回 ViewController 类的新实例/对象,当然有默认值。

如果您从 ViewController 显示 ViewController2 ,您可以在 variable2 ,如变量 > ViewController 并在显示之前设置值,但在创建 ViewController2 之后。如果您使用segues,可以将此代码放在 ViewController 类:

// Put this code in ViewController class
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let viewController2 = segue.destinationViewController as? ViewController2
    if viewController2 != nil {
        // you can't set the value for label at this time 
        // because the viewcontroller and all its UI controls aren't loaded
        // but you can set a non UI variable
        viewController2?.variable2 = self.variable 
    }       
}

之后,您可以在ViewController2类的viewDidLoad方法中放入一行代码:

// Put this code in ViewController2 class
var variable2 = "0"
@IBOutlet var label2Label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    self.label2Label.text = variable2
}