我有一个测试项目可以在多个视图控制器上使用私有数据对象。(我已经从 web 和 git-hub 下载了它)
- (ExampleAppDataObject*) theAppDataObject;
{
id<AppDelegateProtocol> theDelegate = (id<AppDelegateProtocol>) [UIApplication sharedApplication].delegate;
ExampleAppDataObject* theDataObject;
theDataObject = (ExampleAppDataObject*) theDelegate.theAppDataObject;
return theDataObject;
}
第一个问题是,theDelegate 是用 AppDelegateProtocol 强制转换的,即使这个应用程序的 UIApplication 委托名称是 ViewControllerDataSharingAppDelegate,也没有任何警告。我无法理解为什么,也许是因为那是一个 id 类型?(AppDelegateProtocol 是他在 AppDelegate 中声明的自定义委托协议。)
其次,它在每个视图控制器上都显示了这种代码,看起来就像一个单例模式。我不认为这不是在视图控制器之间传输数据的最佳方式。传输对象数据类型的最佳方式是什么?
谢谢。