【发布时间】:2020-10-03 23:09:20
【问题描述】:
喜欢我在这个问题中添加的图像。 我只是做了一个可以在现实生活中使用的计时器。 但有一些问题。 我可以将用户在第二个 setcontroller 中输入的数据扔给 Viewcontroller。 太棒了。但是当我回到视图控制器时,我只想让 viwcontroller'label.text 立即更改。为此,我做了几个功能,例如 viewWillAppear 或 viewdidappear.. 但是那东西不起作用..当我回到视图控制器时,如何立即更改视图控制器?请帮我。 我一直在等待你的意见。(很抱歉我的英语语法。) 我在下面添加我的代码.. 导入 UIKit
class ViewController: UIViewController {
@IBOutlet var AllTileLabel: UILabel!
@IBOutlet var SumTimeLabel: UILabel!
@IBOutlet var CountTimeLabel: UILabel!
@IBOutlet var StartButton: UIButton!
@IBOutlet var StopButton: UIButton!
@IBOutlet var ResetButton: UIButton!
var timeTrigger = true
var realTime = Timer()
var second : Int = 3000
var sum : Int = 0
var allTime : Int = 28800
var IntSecond : Int = 0
var ifReset = false
var data = TimeData()
override func viewDidLoad() {
StartButton.layer.cornerRadius = 10
StopButton.layer.cornerRadius = 10
ResetButton.layer.cornerRadius = 10
// sum = UserDefaults.standard.value(forKey: "sum") as? Int ?? 0
// allTime = UserDefaults.standard.value(forKey: "allTime") as? Int ?? 28800
// second = UserDefaults.standard.value(forKey: "second") as? Int ?? 3000
//
sum = UserDefaults.standard.value(forKey: "sum2") as? Int ?? 0
allTime = UserDefaults.standard.value(forKey: "allTime2") as? Int ?? 28800
second = UserDefaults.standard.value(forKey: "second2") as? Int ?? 3000
AllTileLabel.text = printTime(temp: allTime)
CountTimeLabel.text = printTime(temp: second)
SumTimeLabel.text = printTime(temp: sum)
// getTimeData()
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func StartButtonAction(_ sender: UIButton) {
if timeTrigger { checkTimeTrigger() }
print("Start")
}
@IBAction func StopButtonAction(_ sender: UIButton) {
endGame()
}
@IBAction func ResetButtonAction(_ sender: UIButton) {
getTimeData()
// print("reset Button complite")
second = UserDefaults.standard.value(forKey: "second") as! Int
CountTimeLabel.text = printTime(temp: second)
SumTimeLabel.text = printTime(temp: sum)
// AllTileLabel.text = printTime(temp: allTime)
print("print Time complite")
ifReset = true
}
@IBAction func Reset(_ sender: UIButton) {
endGame()
timeTrigger = true
realTime = Timer()
// getTimeData() //data가 최신화
print("reset Button complite")
second = 3000
sum = 0
allTime = 28800
IntSecond = 0
ifReset = false
AllTileLabel.text = printTime(temp: allTime)
SumTimeLabel.text = printTime(temp: sum)
CountTimeLabel.text = printTime(temp: second)
}
@objc func updateCounter(){
// if String(format: "%.2f",second) == "0.00"{
if second < 1 {
endGame()
CountTimeLabel.text = "종료"
} else {
second = second - 1
sum = sum + 1
allTime = allTime - 1
AllTileLabel.text = printTime(temp: allTime)
SumTimeLabel.text = printTime(temp: sum)
CountTimeLabel.text = printTime(temp: second)
print("update")
UserDefaults.standard.set(sum, forKey: "sum2")
UserDefaults.standard.set(second, forKey: "second2")
UserDefaults.standard.set(allTime, forKey: "allTime2")
}
}
func checkTimeTrigger() {
realTime = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
timeTrigger = false
}
func endGame() {
realTime.invalidate()
timeTrigger = true
}
func printTime(temp : Int) -> String
{
let S = temp%60
let H = temp/3600
let M = temp/60 - H*60
let returnString = String(H) + ":" + String(M) + ":" + String(S)
return returnString
}
func getTimeData(){
second = UserDefaults.standard.value(forKey: "second") as? Int ?? 3000
print("second set complite")
allTime = UserDefaults.standard.value(forKey: "allTime") as? Int ?? 28800
print("allTime set complite")
}
}
【问题讨论】:
标签: ios swift uiviewcontroller uibutton uilabel