ios - 使用 AppDelegate 作为单例

标签 ios objective-c delegates singleton appdelegate

我正在使用我的 AppDelegate 来处理一些音频文件。我已将其设为 AVAudioPlayer 委托(delegate),以便我可以使用 audioPlayerDidFinishPlaying 等方法。

我是这样做的:

@interface AppDelegate : UIResponder <UIApplicationDelegate, AVAudioPlayerDelegate>

然后在任何 ViewController 中,我通过以下方式访问我的 AppDelegate:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

一切正常,但我收到以下警告:

Assigning to 'APPDelegate *__strong' from incompatible type 'id<UIApplicationDelegate>'

最佳答案

您需要将其转换为您的 AppDelegate 类型 -

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

关于ios - 使用 AppDelegate 作为单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22745326/

相关文章:

ios - Xamarin Forms 工具栏/导航栏格式

ios - 如何保存导航 Controller 的状态? (iOS 6 之前)

ios - 在 View Controller 之间传递数据

iphone - Xcode 4.1 有超过 400 个编译错误

ios - 将经过的 TimeInterval 添加到 Date()

c# - 将委托(delegate)从一个类(class)传递到另一个类(class)

iphone - 为 iOS 5.0 编译时什么会导致 "Symbol not found: _objc_setProperty_atomic"错误?

ios - 校准加速度计?

qt - QLineEdit 如何触发 QAbstractItemDelegate::commitData

c# - 隐式和显式委托(delegate)创建之间的区别(有和没有泛型)