我希望有一个能给你时间的功能。我的应用程序中已经有一个时间标签显示当前时间,并且每秒更新一次。但我希望有一个标签,只保留按下按钮的时间。有多个按钮可以将当前时间存储在不同的标签中。
func fetchTime() -> String {
return NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: NSDateFormatterStyle.NoStyle, timeStyle: NSDateFormatterStyle.MediumStyle)
}
@IBAction func clockIn(sender: AnyObject) {
testLabel = fetchTime()
我得到的错误是"使用未解析的标识符'获取时间'
我之前有这个工作,但我不小心删除了应用程序... 我也试过
func fetchTime(label: String) -> String{
label.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: NSDateFormatterStyle.NoStyle, timeStyle: NSDateFormatterStyle.MediumStyle)
}
但是这种情况很糟糕,我理解为什么,而不是如何正确地做到这一点。
答案 0 :(得分:1)
这是你们两个尝试过的工作版......
version1
func fetchTime(label: UILabel){
label.text = "its with parameter"
}
@IBAction func btnClicked(sender: UIButton) {
fetchTime(label)
}
另一个......
func fetchTime() -> String {
return "without parameter"
}
@IBAction func btnClicked(sender: UIButton) {
lbl.text = fetchTime()
}
答案 1 :(得分:0)
您想要获取的是一个字符串,应该将其指定为标签的文本值。
更新
testLabel = fetchTime()
到
testlabel.text = fetchTime()
每次按下按钮时,这将更新标签的值。
答案 2 :(得分:0)
您发布的图片很有帮助。您已在updateTime函数的范围内声明了fetchTime函数。您会注意到updateTime环绕fetchTime的大括号。
在这种情况下,您无法在updateTime之外调用fetchTime。将fetchTime移出updateTime,你就可以了。这就是您看到未定义的函数错误消息的原因。