使用单例在 Watchkit 中传递数据
Using a Singleton to pass data in Watchkit
我之前在 IOS 中使用过 Singleton 并取得了成功,但同样的方法在 watchkit 中对我不起作用...知道为什么我的第二个界面控制器显示 0(初始化值)在第一个接口控制器中输入的数字是多少?
class Singleton {
static let instance = Singleton()
var salesPriceAsInt: Int = 0
}
import WatchKit
import Foundation
class InterfaceController: WKInterfaceController {
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
var salesPrice = ""
@IBOutlet weak var salesPriceLabel: WKInterfaceLabel!
func addCharacter(char: String) {
salesPrice += char
}
@IBAction func zeroButton() {
addCharacter("0")
salesPriceLabel.setText(salesPrice)
}
@IBAction func oneButton() {
addCharacter("1")
salesPriceLabel.setText(salesPrice)
}
@IBAction func twoButton() {
addCharacter("2")
salesPriceLabel.setText(salesPrice)
}
@IBAction func threeButton() {
addCharacter("3")
salesPriceLabel.setText(salesPrice)
}
@IBAction func fourButton() {
addCharacter("4")
salesPriceLabel.setText(salesPrice)
}
@IBAction func fiveButton() {
addCharacter("5")
salesPriceLabel.setText(salesPrice)
}
@IBAction func sixButton() {
addCharacter("6")
salesPriceLabel.setText(salesPrice)
}
@IBAction func sevenButton() {
addCharacter("7")
salesPriceLabel.setText(salesPrice)
}
@IBAction func eightButton() {
addCharacter("8")
salesPriceLabel.setText(salesPrice)
}
@IBAction func nineButton() {
addCharacter("9")
salesPriceLabel.setText(salesPrice)
}
@IBAction func segueButton() {
Singleton.instance.salesPriceAsInt = salesPrice.toInt()!
salesPriceLabel.setText("\(Singleton.instance.salesPriceAsInt)")
}
import WatchKit
import Foundation
class SecondPageInterfaceController: WKInterfaceController {
@IBOutlet weak var totalSaleCommissionLabel: WKInterfaceLabel!
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
}
@IBAction func secondPageButton() {
totalSaleCommissionLabel.setText("\(Singleton.instance.salesPriceAsInt)")
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
事实证明,Singleton 可以工作,但不能启用一个按钮来 (1) 存储到 Singleton 和 (2) segue 到我的第二个界面控制器。为了解决这个问题,我移动了
Singleton.instance.salesPriceAsInt = salesPrice.toInt()!
到 didActivate 方法
我之前在 IOS 中使用过 Singleton 并取得了成功,但同样的方法在 watchkit 中对我不起作用...知道为什么我的第二个界面控制器显示 0(初始化值)在第一个接口控制器中输入的数字是多少?
class Singleton {
static let instance = Singleton()
var salesPriceAsInt: Int = 0
}
import WatchKit
import Foundation
class InterfaceController: WKInterfaceController {
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
var salesPrice = ""
@IBOutlet weak var salesPriceLabel: WKInterfaceLabel!
func addCharacter(char: String) {
salesPrice += char
}
@IBAction func zeroButton() {
addCharacter("0")
salesPriceLabel.setText(salesPrice)
}
@IBAction func oneButton() {
addCharacter("1")
salesPriceLabel.setText(salesPrice)
}
@IBAction func twoButton() {
addCharacter("2")
salesPriceLabel.setText(salesPrice)
}
@IBAction func threeButton() {
addCharacter("3")
salesPriceLabel.setText(salesPrice)
}
@IBAction func fourButton() {
addCharacter("4")
salesPriceLabel.setText(salesPrice)
}
@IBAction func fiveButton() {
addCharacter("5")
salesPriceLabel.setText(salesPrice)
}
@IBAction func sixButton() {
addCharacter("6")
salesPriceLabel.setText(salesPrice)
}
@IBAction func sevenButton() {
addCharacter("7")
salesPriceLabel.setText(salesPrice)
}
@IBAction func eightButton() {
addCharacter("8")
salesPriceLabel.setText(salesPrice)
}
@IBAction func nineButton() {
addCharacter("9")
salesPriceLabel.setText(salesPrice)
}
@IBAction func segueButton() {
Singleton.instance.salesPriceAsInt = salesPrice.toInt()!
salesPriceLabel.setText("\(Singleton.instance.salesPriceAsInt)")
}
import WatchKit
import Foundation
class SecondPageInterfaceController: WKInterfaceController {
@IBOutlet weak var totalSaleCommissionLabel: WKInterfaceLabel!
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
}
@IBAction func secondPageButton() {
totalSaleCommissionLabel.setText("\(Singleton.instance.salesPriceAsInt)")
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
事实证明,Singleton 可以工作,但不能启用一个按钮来 (1) 存储到 Singleton 和 (2) segue 到我的第二个界面控制器。为了解决这个问题,我移动了
Singleton.instance.salesPriceAsInt = salesPrice.toInt()!
到 didActivate 方法