[英]How to control objects in another view controller? (Xcode:objective-c)
I am making a sort of music application and the main view has the playback buttons at the top with other buttons too. 我正在制作一种音乐应用程序,并且主视图的顶部也有播放按钮,其他按钮也是如此。 This section only takes up the top section of the view as the other part has a container view in it.
此部分仅占据视图的顶部,因为另一部分中有容器视图。
I initially had buttons to switch from view to view but I need the audio to be played whilst on different views and to be able to play, pause etc while in the other views 最初,我有一些按钮可以从一个视图切换到另一个视图,但是我需要在其他视图上播放音频,并在其他视图上播放,暂停等
How do I have the button in the main controller act on objects from the child view container? 如何使主控制器中的按钮作用于子视图容器中的对象?
There are a few ways of sending messages from one view controller to another. 有几种方法可以将消息从一个视图控制器发送到另一个。
Since you're building an audio application, using a Singleton might be the best way forward. 由于您正在构建音频应用程序,因此使用Singleton可能是最好的方法。
Your singleton is instantiated once, keeps your playback functionality separate from your view controllers and can therefore be accessed from anywhere within your application. 单例实例化一次,使播放功能与视图控制器分离,因此可以从应用程序中的任何位置进行访问。
Perhaps you have a play
function in your singleton, which starts your audio, and a pause
function that pauses it. 也许您在单身人士中有一个
play
功能可以启动音频,而pause
功能可以将其暂停。 You should be able to call these functions from any view controller. 您应该能够从任何视图控制器调用这些函数。
It would be as simple as: 就像这样简单:
AudioSingleton.shared().play
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.