1

I just Make timer that can use in life. just like image that I push in here, if I go back to main ViewController then I wanna the number that I input in set view controller are tossed to viewController so when I go back to main ViewController and press restart then that number gonna be in text of CountTimeLabel.. but I really don't know how to toss data that I input in another view controller to root viewController... pleas help me.. if I write code like ViewController().variableName = 30 in setViewController, that dose not make things well..(I already know about prepare function but that is not what I am finding..because this is happen when I go back to ViewController(RootViewController)) I will put my code in below.. is it possible to toss data to another view controller from other 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

김미수
  • 61
  • 6

1 Answers1

2

If you're a hobbyist programmer and you just want to "get it done", simply use a static.

Let's say Bottom: UIViewController is the "main", root, view controller at the absolute "base" of your app. no matter what happens "Bottom" is always there.

Say Timer: UIViewController is (any) other view controller you put on top for some reason.

In Bottom, do this

class Bottom: UIViewController, etc ... {
    
    static weak var current: Bottom? = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        Bottom.current = self
    }
    
    func testing() {
        print("it works, WTH")
    }

Note that in ViewDidLoad, you simply set it.

Next, say you are in Timer, try this:

class Timer: UIViewController, etc ... {
    
    func someFunction() {
        Bottom.current.testing()  // it's that easy
    }

It's that easy.


Note there is a huge amount of confusion about using statics, singletons, and similar approaches in iPhone programming.

(Just for example, many engineers will say "avoid singletons!" This is remarkably confused because in iOS engineering, almost everything is a singleton (notably the app itself (!!!!!), the screen, the GPS, etc etc.)

In any event, as a beginner hobbyist, learn how to use statics (it's simple .. Bottom.current. ... as above), and eventually you can learn about the pros and cons of such things.

halfer
  • 19,824
  • 17
  • 99
  • 186
Fattie
  • 27,874
  • 70
  • 431
  • 719