如何让我的柜台在不同的 Viewcontroller

How to get my counter in a different Viewcontroller

我想找回我的 private var currentCount : Int? 这是我的主 ViewController 一秒钟 ViewController (ViewController2)。

可是我抓不到

之后,我想存储此计数器的值以创建一个 "if" 循环来启用和禁用按钮。 谁能帮我?非常感谢

import UIKit

class ViewController: UIViewController
{

    /// Label
    private var customLabel : UILabel?

    /// MAximum Count to which label will be Updated
    private var maxCount : Int?
    /// Count which is currently displayed in Label
    private var currentCount : Int?
    /// Timer To animate label text
    private var updateTimer : Timer?



    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view, typically from a nib.
        customLabel = UILabel()
        customLabel?.textColor = .white
        customLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 25)
        customLabel?.textAlignment = .center


        /// Add label to View
        addConstraints()

        /// Start Timer
        DispatchQueue.main.async {
            self.maxCount = 600
            self.currentCount = 1
            self.updateTimer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(ViewController.updateLabel), userInfo: nil, repeats: true)
        }

    }

    @objc func updateLabel() {
        self.customLabel?.text = String(currentCount!)
        currentCount! += 1
        if currentCount! > maxCount! {
            /// Release All Values
            self.updateTimer?.invalidate()
            self.updateTimer = nil
            self.maxCount = nil
            self.currentCount = nil
        }
    }

    func addConstraints(){
        /// Add Required Constraints
        self.view.addSubview(customLabel!)
        customLabel?.translatesAutoresizingMaskIntoConstraints = false
        customLabel?.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 195).isActive = true
        customLabel?.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50).isActive = true
        customLabel?.heightAnchor.constraint(equalToConstant: 50).isActive = true
        customLabel?.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 310).isActive = true

    }
        }
 //
//  ViewController2.swift
//  PROJET X
//
//  Created by Alexis Decloedt on 22/12/2019.
//  Copyright © 2019 Alexis Decloedt. All rights reserved.
//

import UIKit




class ViewController2: UIViewController {

    @IBOutlet weak var Clue1Button: UIButton!
    @IBOutlet weak var Clue2Button: UIButton!
    @IBOutlet weak var Clue3Button: UIButton!
    @IBOutlet weak var Clue4Button: UIButton!
    @IBOutlet weak var Clue5Button: UIButton!
    @IBOutlet weak var Clue6Button: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()
        Clue1Button.isEnabled = true
        Clue2Button.isEnabled = true
        Clue3Button.isEnabled = true
        Clue4Button.isEnabled = true
        Clue5Button.isEnabled = true
        Clue6Button.isEnabled = true


        let guide = view.safeAreaLayoutGuide
        let height = guide.layoutFrame.size.height

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

    @IBAction func ChestButton(_ sender: Any) {
        dismiss(animated: false, completion: nil)
    }

    }

您似乎没有使用 segue 将信息传输到第二个视图控制器。

您似乎也将变量设为私有。 通过使用私有变量,您无法访问 class 范围之外的变量值。这意味着您无法从第二个视图控制器访问变量。

从变量名中删除 private

所以只是

var currentCount: Int?

Accessing variables from another ViewController in Swift

按照上面的 link 将对您有所帮助

欢迎使用 Whosebug!

我不确定我是否完全理解你的问题,但如果我有你需要:

  • 设置一个变量,您可以从不同的变量更新 查看控制器;
  • 每当变量发生变化时存储它的值;
  • 检查存储的值并执行操作;
    对于最后一步,我不确定您是否需要在一个 vc 或两个 vc 内完成所有操作,因此我将向您展示如何使用两个 vc 来完成。如果您需要一次性完成,请忽略 AppDelegate 部分。

AppDelegate

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var vc1: ViewController?        //Delegate for your first VC
var vc2: SecondViewController?  //Delegate for your second VC

第一个VC

//Set the delegate
let appDelegate = UIApplication.shared.delegate as! AppDelegate
//Declare your global variable
var  currentCount : Int?
//var to shorten the coding proces
var defaults  = UserDefaults.standard
//Key to store the value (store it outside the class to make it global so that it works wherever you want)
let countKey = "CountKey"
//Get the value stored thanks to userDefaults
let currentValue = defaults.integer(forKey: countKey)

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        currentCount = 0 //Init your variable
    }

    func increase(){
        //Increase or decrease your variable here or in a func:
        currentCount! += 1
        //Set the value
        defaults.set(currentCount, forKey: countKey)
    }

    //Function to enable/ disable your button in the other vc
    func buttonStatus(){
        if currentValue == 6{
        appDelegate.vc2?.myButton.isEnabled = true
        } else {
            //...
        }
    }

}

第二个VC

class SecondViewController: UIViewController {

    @IBOutlet weak var myButton: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        //Print the value stored
       print("\n", currentValue)

    }
    //Use this just if you need to use just a view controller
    func getValue(){
        if currentValue == 6 {
            myButton.isEnabled = true
        }
    }

}

拜托,每当您更新计数器时,请记住设置:

 defaults.set(currentCount, forKey: countKey)

如果您需要更多帮助,请告诉我!