gpt4 book ai didi

ios - 当我回到 Viewcontroller 时如何更改 View Controller 的 label.text?

转载 作者:行者123 更新时间:2023-12-01 18:34:30 24 4
gpt4 key购买 nike

就像我在这个问题中添加的图像。
我只是做了一个可以在现实生活中使用的计时器。
但有一些问题。
我可以将用户在第二个 setcontroller 中输入的数据扔给 Viewcontroller。
太棒了。但是当我回到 View Controller 时,我只想让 viwcontroller'label.text 立即更改。为此,我做了几个功能,例如 viewWillAppear 或 viewdidappear..
但是那东西不起作用..当我回到 View Controller 时如何立即更改 View Controller ?请帮我。
我一直在等待你的意见。(很抱歉我的英语语法。)
我在下面添加我的代码.. enter image description here
导入 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 并调用这样的函数时使用委托(delegate)

func updateLbl(_ text:String){}

在第一个vc里面

关于ios - 当我回到 Viewcontroller 时如何更改 View Controller 的 label.text?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62372971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com