【问题标题】:Is it possible to toss data to another view controller?是否可以将数据扔到另一个视图控制器?
【发布时间】:2020-10-03 10:25:42
【问题描述】:

我只是制作可以在生活中使用的计时器。就像我在这里推入的图像一样,如果我回到主 ViewController,那么我想将我在 set view controller 中输入的数字扔到 viewController 中,所以当我回到主 ViewController 并按重新启动时,那个数字将在文本中CountTimeLabel .. 但我真的不知道如何将我在另一个视图控制器中输入的数据扔到根 viewController ......请帮助我.. 如果我在 setViewController 中编写 ViewController().variableName = 30 之类的代码,那剂量做得不好..(我已经知道准备功能,但这不是我发现的..因为当我回到 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.
}
*/

}

enter image description here

【问题讨论】:

标签: ios swift uiviewcontroller


【解决方案1】:

如果您是一名业余程序员并且只想“完成它”,只需使用静态。

假设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. ... 同上),最终您可以了解这些东西的优缺点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 2016-09-06
    • 2020-08-13
    相关资源
    最近更新 更多