就像我在这个问题中添加的图片一样,我只是做了一个可以在现实生活中使用的定时器,但是有一些问题,我可以把用户在第二个设置控制器中输入的数据扔到Viewcontroller中,这很酷,但我只是想让viwcontroller的label.text在我回到View控制器时立即改变。为了实现这一点,我做了几个函数,如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")
}
}
viewWillAppear
viewDidAppear
不调用时,你提出的第二个VC模式,你可以发送数据,当你回来,然后使用委托,当你解散第二个VC和调用这样的函数。
func updateLbl(_ text:String){}
里头
您可以使用闭包来回传数据
class DestinationViewController: UIViewController {
var onCompletion: ((text: String) -> ())? // Add a closure onCompletion
//either back button or dismiss button
@IBAction func someButtonTapped(sender: AnyObject?) {
onCompletion?(text:your text here)
}
}
class ViewController: UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let destinationController = segue.destinationViewController as? DestinationViewController else { return }
destinationController.onCompletion = { text in
// this will be executed when `someButtonTapped(_:)` will be called
print(text)
}
}
}