我只是制造可以在生活中使用的计时器。就像我在此处插入的图像一样,如果我返回到主ViewController,那么我想将我在set View Controller中输入的数字扔给viewController,因此当我返回主ViewController并按重新启动时,该数字将出现在文本中CountTimeLabel ..但我真的不知道如何将我在另一个视图控制器中输入的数据扔给root viewController ...请帮助我..如果我在setViewController中编写类似ViewController()。variableName = 30的代码,那剂量不好使..(我已经知道了prepare函数,但是那并不是我要寻找的。.因为这是在我回到ViewController(RootViewController)时发生的),所以我将代码放在下面。 是否有可能将数据从其他视图控制器扔给另一个视图控制器?
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.
}
*/
}
答案 0 :(得分:2)
如果您是一名业余程序员,并且只想“完成”,只需使用静态。
假设Bottom: UIViewController
是应用程序绝对“基础”上的“主”,根目录,视图控制器。无论发生什么情况,“底部”始终存在。
说Timer: UIViewController
是由于某种原因您放在顶部的(任何)其他视图控制器。
在底部,执行此操作
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. ...
),最终您可以了解此类方法的优缺点。祝你好运!