Swift 中的 WKInterfaceTable、行控制器和按钮操作
WKInterfaceTable, Row Controllers and Button actions in Swift
我在 Swift 中尝试 Apple Watch 开发时遇到了一项相当基本的任务。我最近收到了似乎是 Obj C 中问题的完美答案,但我似乎无法翻译它。
我有一个界面控制器,其中包含一个 table 和同一行控制器的多行。每行包含两个按钮。当点击按钮时,它的背景图像会发生变化,但需要在界面控制器中调用另一个函数 class,并且该函数需要访问界面控制器中的 class 级变量 class.
Table 连接到接口控制器。按钮连接到行控制器 class。这是我简化的 Swift 代码。感谢观看!
/// MY INTERFACE CLASS ///
class MainBoard: WKInterfaceController {
@IBOutlet weak var TileGrid: WKInterfaceTable!
let numRows = 3
let imageNames = ["img1","img2","img3"]
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
initBoard()
}
func initBoard() {
self.TileGrid.setNumberOfRows(self.numRows, withRowType: "Row")
for var i = 0; i < numRows; i++ {
let tmpImageA = UIImage(named: imageNames[0])
let tmpImageB = UIImage(named: imageNames[1])
let rowOfTiles = self.TileGrid.rowControllerAtIndex(i) as Row
rowOfTiles.backImageA = tmpImageA
rowOfTiles.backImageB = tmpImageB
}
}
func doSomething() {
//this function needs to be called whenever a button is pressed
}
}
///////MY ROW CONTROLLER CLASS ////////////////////////
class Row : NSObject {
// At runtime these will be assigned based on the random game board creation
var backImageA = UIImage(named: "default.png")
var backImageB = UIImage(named: "default.png")
@IBOutlet weak var TileA: WKInterfaceButton!
@IBOutlet weak var TileB: WKInterfaceButton!
@IBAction func TapTileA() {
TileA.setBackgroundImage(backImageA)
//NEED TO CALL FUNCTION DO SOMETHING IN INTERFACE CONTROLLER
}
@IBAction func TapTileB() {
TileB.setBackgroundImage(backImageB)
//NEED TO CALL FUNCTION DO SOMETHING IN INTERFACE CONTROLLER
}
}
我的最佳答案是在您的 RowController
class 中创建一个委托,然后在您的 InterfaceController
中实现该委托
委托可以有方法 buttonOneClickHappened
和 buttonTwoClickHappened
然后每个 RowController 都将委托作为 属性,当您创建 RowController 时调用 setDelegate: self
,其中 self
是您的 InterfaceController
希望这对您有所帮助。
我在 Swift 中尝试 Apple Watch 开发时遇到了一项相当基本的任务。我最近收到了似乎是 Obj C 中问题的完美答案,但我似乎无法翻译它。
我有一个界面控制器,其中包含一个 table 和同一行控制器的多行。每行包含两个按钮。当点击按钮时,它的背景图像会发生变化,但需要在界面控制器中调用另一个函数 class,并且该函数需要访问界面控制器中的 class 级变量 class.
Table 连接到接口控制器。按钮连接到行控制器 class。这是我简化的 Swift 代码。感谢观看!
/// MY INTERFACE CLASS ///
class MainBoard: WKInterfaceController {
@IBOutlet weak var TileGrid: WKInterfaceTable!
let numRows = 3
let imageNames = ["img1","img2","img3"]
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
initBoard()
}
func initBoard() {
self.TileGrid.setNumberOfRows(self.numRows, withRowType: "Row")
for var i = 0; i < numRows; i++ {
let tmpImageA = UIImage(named: imageNames[0])
let tmpImageB = UIImage(named: imageNames[1])
let rowOfTiles = self.TileGrid.rowControllerAtIndex(i) as Row
rowOfTiles.backImageA = tmpImageA
rowOfTiles.backImageB = tmpImageB
}
}
func doSomething() {
//this function needs to be called whenever a button is pressed
}
}
///////MY ROW CONTROLLER CLASS ////////////////////////
class Row : NSObject {
// At runtime these will be assigned based on the random game board creation
var backImageA = UIImage(named: "default.png")
var backImageB = UIImage(named: "default.png")
@IBOutlet weak var TileA: WKInterfaceButton!
@IBOutlet weak var TileB: WKInterfaceButton!
@IBAction func TapTileA() {
TileA.setBackgroundImage(backImageA)
//NEED TO CALL FUNCTION DO SOMETHING IN INTERFACE CONTROLLER
}
@IBAction func TapTileB() {
TileB.setBackgroundImage(backImageB)
//NEED TO CALL FUNCTION DO SOMETHING IN INTERFACE CONTROLLER
}
}
我的最佳答案是在您的 RowController
class 中创建一个委托,然后在您的 InterfaceController
委托可以有方法 buttonOneClickHappened
和 buttonTwoClickHappened
然后每个 RowController 都将委托作为 属性,当您创建 RowController 时调用 setDelegate: self
,其中 self
是您的 InterfaceController
希望这对您有所帮助。