Make UIViewController a singleton?

前端 未结 3 1434
南旧
南旧 2020-12-20 21:33

During the use of my app, the user should be able to start, stop, forward background music. (Across several UIViewControllers). For this, I made my MusicPlayer a singleton (

相关标签:
3条回答
  • 2020-12-20 21:48

    You can't : How to add button to UINavigationController

    "Add UIbarButtonItem on the UIViewController not not on the UINavigationController. The navigation controller displays the navigation item of the top viewVontroller, not itself."

    0 讨论(0)
  • 2020-12-20 21:57

    If your singleton MusicPlayer is playing the music, then it should not be interrupted when the view changes. And instead of creating music controls for every view controller, you could add the music controls view as a subview of the window and make sure that it stays on top of everything else.

    Update: In your application delegate, you typically have some code to set up the main view (i.e. the applicationDidFinishLaunching method). I assume that you have a primary navigation or tab controller in which you do everything else. So after adding its view to the window, create and add your music player controller view and add it as a subview of the window. It will remain on top as long as you don't add other views to the window (if you do, you just need to move the music controls back to the top).

    I would use a singleton MusicPlayerController view controller which owns the music player controls view. That way, other view controllers can easily show or hide the controls.

    0 讨论(0)
  • 2020-12-20 22:05

    If you use a container view controller (e.g. UINavigationController), you then have all the view controllers switching between one another, and on top of the container you can add your MusicPlayer controls (easiest way would be for it to have the same parent as the navigation controller), that way it's created only once and it doesn't depend on what views are displayed under it.

    0 讨论(0)
提交回复
热议问题