【发布时间】:2015-03-07 08:32:46
【问题描述】:
我有一个委托视图,我想在按下按钮时在 GameController 中调用 numpadView(numpadView:),但是我无法让它工作。 touchesBegan() 的重载工作正常,它确实与 pressDelegate?.numpadView(self) 一致,它不调用 GameController 类中的委托函数。我不知道要让它工作还缺少什么?
为了简单起见,我清理了代码,只留下与问题相关的基本内容。
NumpadView.swift
protocol NumpadPressDelegateProtocol {
func numpadView(numpadView: NumpadView)
}
class NumpadView: UIButton{
var pressDelegate: NumpadPressDelegateProtocol?
init(char: Character) {
let frame = CGRectMake(160, 100, 50, 50)
super.init(frame:frame)
self.setTitle("\(char)", forState: UIControlState.Normal)
self.userInteractionEnabled = true
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
pressDelegate?.numpadView(self)
}
}
GameController.swift
class GameController: NumpadPressDelegateProtocol {
func numpadView(numpadView: NumpadView) {
//do something
}
}
【问题讨论】: