How to Create a ‘Love’/‘Like' Button

How would I create a 'love' or 'like' button in xcode 6 (Swift)? There are a number of things I want this button to do:


1. Remain 'Liked' even if the user closes the app

2. When clicked displays a red heart instead of the previous gray one

3. Create a new cell on a UITableView from the single View Controller it is displayed on so that it shows the label (in this case) on the cell


Here is what I have so far, I am missing 1 and 3, however I also wanted to know if there are any alternatives to do number 2 from what I am doing here (self.VAR.hidden):


    @IBOutlet var mealName: UILabel!
    @IBOutlet var love: UIButton! //Gray heart
    @IBOutlet var loveRed: UIButton! //Red heart (new image)

override func viewDidLoad() {
        super.viewDidLoad()

        if (self.loveRed.hidden == false) {  //I want it so that it shows a gray heart unless the user has previously
            self.loveRed.hidden = false      //tapped the red heart to like the post. When I run the app, it always just
            self.love.hidden = true          //shows a the red heart.
        }
        else {
            self.loveRed.hidden = true
            self.love.hidden = false
        }

}


//Outside viewDidLoad()

    @IBAction func lovePressed(sender: AnyObject) { //When user taps the gray heart
        self.loveRed.hidden = false //the red heart shows
        self.love.hidden = true //and the gray heart hides

        //Here is where I would put the code to step #3
    }
    @IBAction func lovePressedRed(sender: AnyObject) { //When user taps the red heart
        self.loveRed.hidden = true //the red heart hides
        self.love.hidden = false //and the gray heart shows

       //And here is where I would put the code to remove the mealName label from step #3
    }


Any help would be very much appreciated and I would be very grateful.

I think you could replace the two buttons with just one and use the state to show the differences. For example, a button can show a different image or title depending on the state of the button. If you are just storing one state for the entire app, you could use NSUserDefaults otherwise if it is for a list of items I would recommend using Core Data.


Here is a simple example changing the button title per selected or normal state and storing the state in the user defaults.


  @IBOutlet var likesButton : UIButton!
  /
  var likes : Bool {
       get {
            return NSUserDefaults.standardUserDefaults().boolForKey("likes") ?? false
       }
       set {
            NSUserDefaults.standardUserDefaults().setBool(newValue, forKey: "likes")
       }
  }

  @IBAction func toggleMode(sender: AnyObject) {
       // toggle the likes state
       self.likes = !self.likesButton.selected
       // set the likes button accordingly
       self.likesButton.selected = self.likes
  }

  override func viewDidLoad() {
       super.viewDidLoad()

       // initialize the likes button to reflect the current state
       self.likesButton.selected = self.likes

       // set the titles for the likes button per state
       self.likesButton.setTitle("Like", forState: .Normal)
       self.likesButton.setTitle("Likes", forState: .Selected)
  }
How to Create a ‘Love’/‘Like' Button
 
 
Q