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")
}
}
您可以使用闭包传递回数据
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)
}
}
}
当您以模式显示第二个vc时,ViewWillAppease
/ViewDidAppease
不会被调用,因为您可以在返回时发送数据,然后在您取消第二个vc时使用委托并调用如下函数
func updateLbl(_ text:String){}
在第一个vc内部
对于更改标签,您可以通过init()使用单向通信
// call this from the view controller you want to send the values from i.e. Set View Controller
_ = ViewController(label1: self.minutesLabel.text!, label2: self.secondsLabel.text!)
现在在类内部的ViewController中
init(label1: String?, label2: String?) {
super.init(nibName: nil, bundle: nil)
self.label1_In_ViewController.text = label1!
self.label2_In_ViewController.text = label2!
}
只需在这些函数中更改标签的名称和标签的数量,使其全部工作!