我正在开发一个简单的计时器应用程序。我已经正确设置了我的swift桥接头并在我的View Controller类中声明了委托协议
class SecondViewController: UIViewController, MZTimerLabelDelegate {
然后,在viewDidLoad()
之后,我实现了委托的协议:
func timerLabel(timerLabel: AnyObject!, finshedCountDownTimerWithTime countTime: NSTimeInterval){
self.startTimer.setTitle("Ok!", forState: .Normal)
}
我的MZTimerLabel
实例名为brewTimer
。我不知道在哪里设置brewTimer.delegate = self
。我也有时把它看作是var delegate: myDelegate = //code here
,我根本不理解。我在Swift中使用委托可以找到的所有样本都与两个View Controller之间的转换有关,这不是我想要做的。
我想点击我的应用中的“开始”按钮,让应用在定时器完成时更改按钮的标签。启动计时器的我的计时器代码是:
else {
brewTimer.start()
buttonSelect = 1
startTimer.setTitle("Reset", forState: .Normal)
//Start the circle counter graphic
circleCounterOuter.startWithSeconds(5)
circleCounterInner.startWithSeconds(2)
timerLabel(brewTimer, finshedCountDownTimerWithTime:5)
}
该函数的最后一行是我调用代理协议函数的地方,但是当我点击"开始"标签立即变为" OK!"而不是等待finishedCountDownTimerWithTime
的NSTimeInterval为5.如果有人在想,我会使用cocoapod MZTimerLabel。
答案 0 :(得分:0)
我终于通过用户对不同问题的输入来解决这个问题。我读到Apple link的代表团,但实施仍然令人困惑。我理解它的方式,我不得不告诉我的MZTimerLabel实例在我希望委托开始的代码中“委托”。只要我在View Did Load中声明brewTimer.delegate = self
某处并且我的委托代码将在我的计时器运行时执行,我就会自动发生这种情况,但显然情况并非如此。结果我不得不把brewTimer.delegate = self
放在告诉我的计时器开始计数的函数的末尾(我的开始按钮功能):
else {
brewTimer.start()
buttonSelect = 1
startTimer.setTitle("Reset", forState: .Normal)
//Start the circle counter graphic
circleCounterOuter.startWithSeconds(5)
circleCounterInner.startWithSeconds(2)
//set the delegate
brewTimer.delegate = self
}
所以我看到它的方式,brewTimer.delegate = self
语句告诉我执行“特殊”委托函数。