2017-04-20 29 views
0

我已經添加了一個按鈕,它爲標籤添加了點。 一切工作正常,然後標籤被保存到核心數據並出現在tableViewCell中。遞增點回到0 swift

當我回到我的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 

     } 

} 
+0

不能看到更多的代碼或你的過程,我認爲這是因爲你可能將標籤設置爲currentScore,我也猜測該文件加載時爲0。因此,無論CoreData中存儲什麼文件,只要該文件加載,它的初始設置爲0。 – Asdrubal

+0

感謝@Asdrubal,這裏是我的更多代碼 –

+0

您需要更改的行是var currentScore = 0.無論CoreData中的值是0還是0,它都應該被設置爲相等。類似於'currentScore = coreDataValue? 0' – Asdrubal

回答

0

當你得到你還應該設置currentScore爲該值(如果大於0)的持久號碼。我相信目前你只能將它設置爲0,這就是增量重新開始的原因。

+0

非常感謝您的幫助Ahmad。我編輯我的帖子更多的細節..我是新來的核心數據,我卡住了。在loadGoalData()中需要很多 –

+0

,您需要設置currentScore = goal.plusOnes。 – Ahmad

+0

非常感謝艾哈邁德。這幾乎做了訣竅。我非常感謝你的幫助 –