我添加了一个按钮,将点添加到标签。
一切正常,然后将标签保留到核心数据中,并显示在tableViewCell中。

当我返回到detailsVC时,我得到带有持久编号的标签,但是当我再次单击该按钮以增加点数时,标签又回到零。

这是我的代码的一部分:

import UIKit
import CoreData

class GoalDetailsVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    // IBOutlets:

    @IBOutlet weak var titleTF: UITextField!
    @IBOutlet weak var detailsTextView: UITextView!
    @IBOutlet weak var pointsLabel: UILabel!
    @IBOutlet weak var dateOfEntry: UILabel!
    @IBOutlet weak var thumbImage: UIImageView!


    // properties

    var currentScore = 0
    var goalToEdit: Goal? // goalToEdit is now an optional, and it needs to be unwrapped when used.
    var imagePicker: UIImagePickerController!



    override func viewDidLoad() {
        super.viewDidLoad()

        if let topItem = self.navigationController?.navigationBar.topItem {
            topItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: nil, action: nil)

        }

        // now we need to say that if there is a goal to edit ( not equal to nil), then we load the Goal data with the loadGoalData() function.



        if goalToEdit != nil {

            loadGoalData()
        }

        imagePicker = UIImagePickerController()
        imagePicker.delegate = self



    }

    // when button is pressed, I need to
    // 1 : add a point to the pointsLabel
    // 2 : put the current date to the dateLabel
    // 3 : persist the new points and date labels.

    @IBAction func plusOneBtnPressed(_ sender: UIButton) {

        currentScore += 1

        pointsLabel.text = "\(currentScore)"

    }


    @IBAction func minusOneBtnPressed(_ sender: Any) {
    }


    @IBAction func savePressed(_ sender: Any) {

        var goal: Goal!
        let picture = Image(context: context) // Image = Entity

        picture.image = thumbImage.image // image = attribute



        if goalToEdit == nil {

            goal = Goal(context: context)
        } else {
            goal = goalToEdit
        }


        goal.toImage = picture

        // this is unwrapping because the original goalToEdit is an optional.

        if let title = titleTF.text {
            goal.title = title

        }

        // we saveed, or persisted the TITLE

        if let points = pointsLabel.text {
            goal.plusOnes = (points as NSString).intValue
        }


        // we saveed, or persisted the POINTS

        if let details = detailsTextView.text {

            goal.details = details
        }

        // we saved, or persisted the DETAILS

        let dateFormatter = DateFormatter()

        dateFormatter.dateFormat = "EEEE MMM d yyyy"

        if let date = dateFormatter.date(from: dateFormatter.dateFormat) {
            goal.lastEntry = date as NSDate
        }

        // we saved, or persisted the DATE

        ad.saveContext()

        _ = navigationController?.popViewController(animated: true)

    }

    func loadGoalData() {

        if let goal = goalToEdit {
            titleTF.text = goal.title
            pointsLabel.text = "\(goal.plusOnes)"
            detailsTextView.text = goal.details
            dateOfEntry.text = (String(describing: goal.lastEntry))
            thumbImage.image = goal.toImage?.image as? UIImage

        }

}

最佳答案

当获得持久编号时,还应该将currentScore设置为该值(如果大于0)。我相信目前您仅将其设置为0,这就是增量重新开始的原因。

关于swift - 增量点回到0 swift ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43521108/

10-13 03:43