【发布时间】:2017-11-21 06:30:32
【问题描述】:
我有一个简单的应用程序。它有一个按钮,它加载一个名为 Info 的 View Controller,其中包含有关应用程序的信息。在 Info View Controller 上有一个“Go Back”按钮,可将用户带回主 View Controller。在主屏幕上,有一个名为 countLabel 的标签,其中包含一个 Int,当转到信息屏幕并返回该 Int 的值时,它会重置为默认值 0。如何在另一个屏幕上保留该 Int 的值?
// ViewController.swift
// Simple Count
//
// Created by Jamie King on 11/19/17.
// Copyright © 2017 Jamie King. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var countValue = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBOutlet weak var countLabel: UILabel!
@IBAction func countButton(_ sender: Any) {
countValue = countValue + 1;
update()
}
@IBAction func minusButton(_ sender: Any) {
countValue = countValue - 1
update()
}
@IBAction func resetButton(_ sender: Any) {
countValue = 0
update()
}
@IBAction func infoButton(_ sender: Any) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func update(){
countLabel.text = "\(countValue)"
}
}
【问题讨论】:
-
尝试使用
self.countValue而不是countValue -
添加了 self.countValue 但从另一个视图控制器返回时似乎没有保留该值。
-
您是否将其添加到您的
update()func 中?countLabel.text = "\(self.countValue)" -
是的,我已经更新了更新函数以包含 self.countValue
-
标签: ios swift view controller