0

Let's say I has a audio player app and there is song list (tableView) in VC1 to present all the songs and clicking any row of it to open another VC2 to present the playing scene. In this playing scene there is some buttons for playing sequence (repeat, repeatOne, shuffle).

Since user will frequently dismissed/popup this VC2 to get back to VC1 (song list), I store the playing sequence button value to another file called SongData.swift and define a static var static var repeatOneSequence = false to store the value of VC2. Then retrieve it if SongData.repeatOneSequence == true like this in VC2.

Now it is working, but it is just my way...I didn't get info about how to do it properly. I'm wondering if there is other way to store the data of dismissed VC2 or which way is the better one to do it.

/ / / Insert code snip for reference

    @IBAction func repeatButton(_ sender: UIButton) {
        if SongData.repeatOneSequence == true {
            print("repeatOneSequence TRUE!")
            repeatButton.setImage(UIImage(named: "icons8-repeat-50"), for: .normal)
            repeatButton.imageView?.contentMode = .scaleAspectFit
            SongData.repeatOneSequence = false
        } else {
            print("repeatOneSequence FALSE!")
            repeatButton.setImage(UIImage(named: "icons8-repeat-one-50"), for: .normal)
            repeatButton.imageView?.contentMode = .scaleAspectFit
            SongData.repeatOneSequence = true
        }
    }
Zhou Haibo
  • 1,681
  • 1
  • 12
  • 32
  • See https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?r=SearchResults&s=2|154.7031. Be sure to look at several of the answers, not just the first. – rmaddy Oct 08 '19 at 03:35
  • rmaddy, thanks, it is a long thread but really worth to read, well per my current knowledge of Swift, I couldn't understand all of it, but some points and approaches is helpful to me, could know that many way to transfer data in Swift. It seems that send value back to VC1 when dismiss VC2(use protocol and delegate) could be preferred way, but a bit complicate code. And my current way is to save data of VC2 into my Model class. – Zhou Haibo Oct 08 '19 at 04:15

0 Answers0