Mega4alik
Mega4alik

Reputation: 609

Swift. Transition between ViewControllers without dismissing them on going back

I'm trying build an app which has the same navigation structure like iTunes. enter image description here

When user clicks "Now Playing"(3rd window) button, SecondViewController(assume 2nd window) should be opened. When user goes back to previous ViewController, app should be able to keep SecondViewController alive(music is playing in the background) in order to access it any time from any ViewController.

How to make such hierarchy using Navigation or any other Controllers? Hopefully, the question is clear.

Upvotes: 0

Views: 653

Answers (2)

Aggressor
Aggressor

Reputation: 13551

Follow @Rikkles advice.

To add specifically to your issue you should look at the Model-View-Controller structure. http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Regarding the views, what you need to do is have a UIViewController for each view you want. Then when you want another view to appear on top you present one UIViewController on another:

ROOT_VIEW_CONTROLLER.presentViewController(controller, animated: true, completion: {})

and to dismiss it

ROOT_VIEW_CONTROLLER.dismissViewControllerAnimated(true, completion: nil)

For the full docs on UIViewController's look here (it's well worth a read): https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

Upvotes: 2

Rikkles
Rikkles

Reputation: 3372

You should consider view controllers to be just that: Controllers that manage views. You're tying a function (playing a specific song) to a user interface view (seeing its picture, time, volume).

Your hierarchy question is therefore not the correct question to ask. You should be instead considering how to make a class (ideally a singleton, call it CurrentlyPlayingMusic) that manages the music that currently plays, and when the user interface needs to display the currently playing music, it loads the SecondViewController from your example which, at that point, requests from the CurrentlyPlayingMusic the data and metadata necessary to display what it needs: song name, artist, time, etc...

CurrentlyPlayingMusic should be a singleton that, once instanciated, stays resident in memory and properly plays the music. Any time you need to know what is happening, or you need to act upon the currently playing music (start/stop/etc...), you query CurrentlyPlayingMusic. In a sense it's a light wrapper over AVAudioPlayer that also lets you put in your own properties that you want to persist over the life of that music.

Upvotes: 3

Related Questions