【发布时间】:2012-05-08 03:40:15
【问题描述】:
此时我有一个相当简单的 iOS 5 应用程序。
UINavigationController 被设置为 Interface Builder 中的初始视图控制器(我们仍然称它为 Interface Builder 吗?无论如何...)。 UINavigationController 有一个通用的 UITabBarController 作为它的 rootViewController。
我想将主 AppDelegate 用于 UINavigationController 和 UITabBarController 委托。我使 AppDelegate 符合 UINavigationControllerDelegate 和 UITabBarControllerDelegate。一切正常。但是!
在 AppDelegate 的 application:didFinishLaunchingWithOptions: 中,我觉得我在做一些 hacky。为了设置每个控制器委托,我必须对 self.window.rootViewController 和 self.window.rootViewController.topViewController 进行类型转换。
UINavigationController *rootNavigationController = (UINavigationController *) self.window.rootViewController;
UITabBarController *rootTabBarController = (UITabBarController *) rootNavigationController.topViewController;
[rootNavigationController setDelegate:self];
[rootTabBarController setDelegate:self];
如果我不进行类型转换 self.window.rootViewController 我无法设置它的委托。 topViewController (UITabBarController) 也一样。
如果我在 Inferface Builder 中声明初始视图是一个 UINavigationController,而不是一些通用的 UIViewController,我为什么还要对所有内容进行类型转换?这是 hacky 吗?
【问题讨论】:
-
你在尝试
[self.window.rootViewController setDelegate:self]时得到了什么? -
"'UIViewController' 没有可见的@interface 声明选择器 setDelegate。"它不会编译。
标签: objective-c ios cocoa-touch ios5