我有两个视图控制器。我还有一个名为number
的通用变量。第一个视图控制器上有一个名为mainLabel
的标签。我的第二个视图控制器上有一个按钮。按下按钮时,它应从变量number
中减去200,然后更新mainLabel
标签。我无法弄清楚如何使mainLabel
标签在第二个视图控制器上运行。
import UIKit
var number:Int = 0
class ViewController: UIViewController {
@IBOutlet weak var mainLabel: UILabel!
@IBAction func backgroundButton(sender: AnyObject) {
number = number + 1
mainLabel.text = "\(number)"
NSUserDefaults.standardUserDefaults().setObject(number, forKey: "number")
}
override func viewDidLoad() {
super.viewDidLoad()
if NSUserDefaults.standardUserDefaults().objectForKey("number") != nil {
number = NSUserDefaults.standardUserDefaults().objectForKey("number") as! Int
}
mainLabel.text = "\(number)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var buy1Label: UILabel!
@IBAction func buy1(sender: AnyObject) {
number = number - 200
buy1Label.text = "Bought!"
mainLabel.text = "\(number)"
}
}
答案 0 :(得分:0)
class ViewController: UIViewController {
@IBOutlet weak var mainLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NSNotificationCenter.defaultCenter().addObserverForName("NSNotificationName", object: nil, queue: nil) { (note) -> Void in
// Number changed, update your UILabel.
var number: Int = NSUserDefaults.standardUserDefaults().integerForKey("keyOfNumber")
self.mainLabel.text = "\(number)"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func subtractNumberBy200() {
var number: Int = NSUserDefaults.standardUserDefaults().integerForKey("keyOfNumber")
number -= 200
NSUserDefaults.standardUserDefaults().setInteger(number, forKey: "keyOfNumber")
}
}
希望这对你有用。
答案 1 :(得分:-1)
首先,设置一些常见的全局事物。
let NumberDidChangeNote = "NumberDidChangeNote" // A name for the notification.
// A function to get the value of number the same way every time.
func number() -> Int {
return NSUserDefaults.standardUserDefaults().integerForKey("number");
}
// A function to set the value of number the same way every time.
func setNumber(number: Int) {
NSUserDefaults.standardUserDefaults().setInteger(number, forKey: "number")
NSUserDefaults.standardUserDefaults().synchronize()
// Post a number did change notification
NSNotificationCenter.defaultCenter().postNotificationName(NumberDidChangeNote, object: nil)
}
第一个视图控制器分割设置编号和设置标签的角色。
class ViewController: UIViewController {
@IBOutlet weak var mainLabel: UILabel!
@IBAction func backgroundButton(sender: AnyObject) {
setNumber(number() + 1)
}
dynamic func numberDidChange(note: NSNotification) {
mainLabel.text = "\(number())"
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Add an observer which will call numberDidChange() anytime the number changes.
let center = NSNotificationCenter.defaultCenter()
center.addObserver(self, selector: "numberDidChange", name: NumberDidChangeNote, object: nil)
// Fake an initial notification to numberDidChange() to set the initial value of the label.
numberDidChange(NSNotification(name: NumberDidChangeNote, object: nil))
}
override func viewWillDisappear(animated: Bool) {
// Remove the observer when not on the screen.
let center = NSNotificationCenter.defaultCenter()
center.removeObserver(self, name: NumberDidChangeNote, object: nil)
super.viewWillDisappear(animated)
}
}
现在第二个视图控制器根本不用担心第一个视图控制器。
class SecondViewController: UIViewController {
@IBOutlet weak var buy1Label: UILabel!
@IBAction func buy1(sender: AnyObject) {
setNumber(number() - 200)
buy1Label.text = "Bought!"
}
}