如何在其他Viewcontroller中获得我的计数器

问题描述 投票:0回答:2

我想找回我的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)
    }

    }
ios swift counter viewcontroller
2个回答
1
投票

看来您不是在使用segue将信息传输到第二个视图控制器。

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

从您的变量名中删除私有

就是这样

var currentCount: Int?

Accessing variables from another ViewController in Swift

按照上面的链接将为您提供帮助


0
投票

欢迎使用StackOverflow!

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

  • 设置一个变量,您可以从其他变量进行更新ViewControllers;
  • 在变量更改时存储其值;
  • 检查存储的值并执行操作;对于最后一步,我不确定是否需要在一两个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)

让我知道是否需要更多帮助!

© www.soinside.com 2019 - 2024. All rights reserved.