G
G
Gleb2021-03-16 12:32:23
Swift
Gleb, 2021-03-16 12:32:23

How to add objects to favorites?

There is a pickerView, with the help of which I select an object (company stock) and display information about it.
I need to add a favorites button, I added it to the storyboard.
I can't figure out how to implement the functionality of the "favorites" button?

as a result, I want to make two buttons:
1) Add/remove from favorites with changing the picture on click
2) View a list of my favorite objects

If you have come across this, give a link to read how to do this.

That is, I have a dictionary, I need to create a container where I will put them on the click of a button, on the object selected in the picker.
Next, I need to create a second scene where I will view a list of my favorite objects.
Would such an approach be optimal?

object parsing:

func parseQuote(form data: Data) {
        do {
            let jsonObj = try JSONSerialization.jsonObject(with: data)
            
            guard
                let json = jsonObj as? [String: Any],
                let companyName = json["companyName"] as? String,
                let companySym = json["symbol"] as? String,
                let price = json["latestPrice"] as? Double,
                let priceChange = json["change"] as? Double else { return print("invalid")
            }
            
            DispatchQueue.main.async { [weak self] in
                self?.displayStockInfo(companyName: companyName,
                                       companySym: companySym,
                                       price: price,
                                       priceChange: priceChange)
            }
        } catch {
            self.errorHandler(withTitle: "JSON error", andMessage: "error with json data: " + error.localizedDescription)
        }
    }


So far I have added only one picture to the button, I did not find how to change it by pressing it. "star" (star.png) to "empty star" (unStar.png) .

favoriteButton.setImage(UIImage(named: "star.png"), for: .normal)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
briahas, 2021-03-16
@shevzoom

So far I have added only one picture to the button, I did not find how to change it by pressing it.

The button has two states - Selected and Unselected. You can attach your own picture to each state. By changing states, you change the picture.
I can't think of a way to implement the functionality of the "favorites" button?
Understand, I also came from the exact sciences, from physics, and at school I was always "bad" in language. BUT, you need to write competently in Russian not because of grades, but in order to be understood correctly.
To be honest, I didn't understand much from your opus.
  1. What method can be used to implement the functionality of the "favorites" button - ? What exactly is the problem? Read favorites from a dictionary? Send the button click event to where? ... 0_o
  2. as a result I want to make two buttons: - ? Do you know how to put two buttons on a view? You don't know how to "Add/remove from favorites with image change on click"? Or all together?
  3. Would this approach be optimal? - ?? Optimal in what parameters? By the speed of work? By scalable architecture? Or in general "are you thinking in the right direction"?

Please treat people who help you for nothing with the same respect you would like to be treated with. And ask clear and specific questions. With examples as clear as possible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question