我有看法。 当我点击btnStartWork时,我想将按钮设置为隐藏(工作),标签startTime显示开始时间(工作)并且btnEndWork显示(工作)。 当我点击btnEndWork时,我没有隐藏任何按钮,btnEndWork仍在显示。他还将结束时间设置为endTime标签,但也更新了startTime。这真的很有线。
class FirstViewController: UIViewController {
@IBOutlet weak var startTime: UILabel!
@IBOutlet weak var endTime: UILabel!
@IBOutlet weak var btnStartWorkLabel: UIButton!
@IBOutlet weak var btnEndWorkLabel: UIButton!
@IBAction func btnStartWork(_ sender: Any) {
let currentdate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
let convertedDate = dateFormatter.string(from: currentdate)
startTime.text = "\(convertedDate)"
btnStartWorkLabel.isHidden = true
btnEndWorkLabel.isHidden = false
}
@IBAction func btnEndWork(_ sender: Any) {
let currentdate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
let convertedDate = dateFormatter.string(from: currentdate)
endTime.text = "\(convertedDate)"
btnStartWorkLabel.isHidden = false
btnEndWorkLabel.isHidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
更新问题
如果点击btnEndWork,我怎么能节省时间,兼顾两次?
答案 0 :(得分:1)
听起来有点连接错误,请查看接口构建器中的控制器以获取多个连接(如果您复制并粘贴标签或按钮,也会复制连接)并尝试添加:
print("Start button pressed")
到
@IBAction func btnStartWork(_ sender: Any)
和
print("End button pressed")
到
@IBAction func btnEndWork(_ sender: Any)
按下按钮时查看输出是否正确。