gpt4 book ai didi

ios - 是否可以将数据扔到另一个 View Controller ?

转载 作者:行者123 更新时间:2023-12-01 19:31:53 25 4
gpt4 key购买 nike

我只是制作可以在生活中使用的计时器。就像我在这里插入的图像一样,如果我回到主 ViewController,那么我想将我在 set view controller 中输入的数字扔到 viewController 中,所以当我回到主 ViewController 并按重新启动时,那个数字将在文本中CountTimeLabel .. 但我真的不知道如何将我在另一个 View Controller 中输入的数据扔到根 viewController ......请帮助我.. 如果我在 setViewController 中编写 ViewController().variableName = 30 之类的代码,那剂量做得不好..(我已经知道准备功能,但这不是我发现的..因为当我回到 ViewController(RootViewController) 时会发生这种情况)我将把我的代码放在下面..
是否可以将数据从其他 View Controller 扔到另一个 View Controller ?

import 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 = 14400
var allTime : Int = 14400
var IntSecond : Int = 0
var ifReset = false

override func viewDidLoad() {
StartButton.layer.cornerRadius = 10
StopButton.layer.cornerRadius = 10
ResetButton.layer.cornerRadius = 10
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) {
print(second)
getTimeData()
//second = 3000
//CountTimeLabel.text = "0:50:00"
CountTimeLabel.text = printTime(temp: second)
ifReset = true
}
@IBAction func Reset(_ sender: UIButton) {
endGame()
timeTrigger = true
realTime = Timer()
second = 3000
sum = 14400
allTime = 14400
IntSecond = 0
ifReset = false

AllTileLabel.text = "8:00:00"
SumTimeLabel.text = "0:0:0"
CountTimeLabel.text = "0:50:00"
}

@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")
}
}

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 = 20
sum = SetViewController().real.sum
allTime = SetViewController().real.allTime
print(second)
}

}











import UIKit

class SetViewController: UIViewController {
@IBOutlet var View1: UIView!
@IBOutlet var View2: UIView!
@IBOutlet var InputView1: UIView!
@IBOutlet var InputView2: UIView!
@IBOutlet var SetButton: UIButton!

@IBOutlet var H1TextField: UITextField!
@IBOutlet var M1TextField: UITextField!
@IBOutlet var H2TextField: UITextField!
@IBOutlet var M2TextField: UITextField!

override func viewDidLoad() {

super.viewDidLoad()
H1TextField.keyboardType = .numberPad
M1TextField.keyboardType = .numberPad
H2TextField.keyboardType = .numberPad
M2TextField.keyboardType = .numberPad

View1.layer.cornerRadius = 14
View2.layer.cornerRadius = 14
InputView1.layer.cornerRadius = 10
InputView2.layer.cornerRadius = 10
SetButton.layer.cornerRadius = 10

// Do any additional setup after loading the view.
}

@IBAction func SetButton(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}

enter image description here

最佳答案

如果您是一个业余程序员并且只想“完成它”,只需使用静态。
假设 Bottom: UIViewController是应用程序绝对“基础”的“主”、根、 View Controller 。无论发生什么,“底部”始终存在。
Timer: UIViewController是(任何)您出于某种原因放在顶部的其他 View Controller 。
在底部,执行此操作

class Bottom: UIViewController, etc ... {

static weak var current: Bottom? = nil

override func viewDidLoad() {
super.viewDidLoad()
Bottom.current = self
}

func testing() {
print("it works, WTH")
}
请注意,在 ViewDidLoad 中,您只需设置它。
接下来,假设您在 Timer , 试试这个:
class Timer: UIViewController, etc ... {

func someFunction() {
Bottom.current.testing() // it's that easy
}
就这么容易。

注意有一个 大量的困惑关于在 iPhone 编程中使用静态、单例和类似方法。
(例如,许多工程师会说“避免使用单例!”这非常令人困惑,因为在 iOS 工程中,几乎所有东西都是单例(尤其是应用程序本身(!!!!!!)、屏幕、GPS 等。 )
无论如何,作为初学者爱好者,学习如何使用静态(很简单.. Bottom.current. ... 同上),最终您可以了解这些东西的优缺点。

关于ios - 是否可以将数据扔到另一个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62363987/

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