0

我有一个带有选项卡控制器的应用程序,它显然有一定数量的选项卡。TabController 中的每个 ViewController 都共享一些信息,所以我决定将这些信息移到 AppDelegate 中并合成 NSDictionary 中的所有内容,以便我可以访问那些使用

[[[UIApplication sharedApplication] delegate] sharedInformations];

该解决方案运行良好,我想它非常好(我接受更好的解决方案)。显然,编译器警告我 [[UIApplication sharedApplication] delegate] 可能不会响应方法 sharedInformations 因为他没有在协议中找到该方法,但我知道它会。

问题是:如何抑制该警告?

4

1 回答 1

2

你可以投它:

[(MyAppDelegate *)[[UIApplication sharedApplication] delegate] sharedInformations];

或稍微整洁:

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
… = appDelegate.sharedInformation;

您还可以将共享信息封装在 Singleton 类中并执行类似的操作

#import "MySharedInfo.h"

MySharedInfo *sharedInfo = [MySharedInfo sharedInfo];
// etc.
于 2011-04-06T11:04:24.183 回答