简体   繁体   English

如何在另一个视图控制器中控制对象? (Xcode:objective-c)

[英]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. 有几种方法可以将消息从一个视图控制器发送到另一个。

  1. Singleton. 单身人士。
  2. Delegates. 代表们
  3. KVO. KVO。
  4. Notifications. 通知。

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.

相关问题 Objective-C以编程方式在另一个视图控制器上显示一个视图控制器 - Objective-C Showing a view controller on another view controller programmatically 当我尝试在另一个视图控制器中访问数据时,为什么它为null? (Objective-C,XCode) - Why is it null when I try to access data in another view controller? (Objective-C, XCode) iOS / xcode / objective-c:如何在注册后实例化视图控制器 - iOS/xcode/objective-c: How to instantiate view controller after signup 使用Objective-C类别时如何导入另一个视图控制器头文件? - How to import another view controller header file when I use Objective-C category? ios / xcode / objective-c:导航到嵌入在导航控制器中的模态视图控制器 - ios/xcode/objective-c: Segue to modal view controller embedded in navigation controller 如何在Objective-c的页面视图控件中设置页码 - How to set page number in Page View Control in Objective-c 如何在objective-c中导航到新故事板文件上的视图控制器 - How to navigate to a view controller on a new storyboard file in objective-c 如何从iOS Objective-C中的控制器重绘自定义视图 - how to redraw custom view from the controller in ios objective-c 如何在 Objective-C 中单击 UILabel 上的超链接打开视图 controller - How to open a view controller on click of hyperlink on UILabel in Objective-C 在Objective-C Xcode 5中以编程方式创建视图 - Create View Programmatically in Objective-C Xcode 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM