如何在不同的视图控制器中获取我的计数器



我想找回我的private var currentCount : Int?在我的主视图控制器上 在第二个视图控制器 (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)
}
}

欢迎来到StackOverflow!

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

  • 设置一个可以从不同的变量更新的变量 视图控制器;
  • 每当变量更改时存储变量的值;
  • 检查存储的值并执行操作;
    对于最后一步,我不确定您是否需要在一两个 vc 中完成所有操作,因此我将向您展示如何使用两个 vc 执行此操作。如果您只需要在一个中执行此操作,请忽略 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

第一个风险投资

//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 {
//...
}
}
}

第二个风险投资

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)

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

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

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

从变量名称中删除私有

所以这只是

var currentCount: Int?

在 Swift 中从另一个 ViewController 访问变量

点击上面的链接将帮助您

最新更新