递增点回到0迅速

时间:2017-04-20 13:34:39

标签: swift core-data

我添加了一个按钮,可以为标签添加点数。 一切正常,然后标签被持久化为核心数据并出现在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

        }

}

1 个答案:

答案 0 :(得分:0)

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