2012-05-18 67 views
0

以我TheTabBarController,而不符合UINavigationControllerDelegate协议,我可以分配我的类moreNavigationController.delegate。实施方法不符合协议

// without conforming to protocol, <UINavigationControllerDelegate> 
@interface TheTabBarController : UITabBarController 

self.moreNavigationController.delegate = self; 

它只是引发下面的警告,但编译成功。

分配到 'TheTabBarController * const的__strong'

这种协议的方法在运行时调用没有任何错误从不相容 类型的 'id'。我用它来隐藏一些视图控制器的更多导航栏。

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 

所以,我想知道,这是合法的,安全的或;它会在以后崩溃还是泄漏内存?这怎么可能在语义上被允许?运行时如何解决该方法,尽管它在界面中没有定义并且协议不符合?或者,UITabBarController使用符合协议的隐藏类别?

回答

1

协议没有意义运行。只有在编译过程中才会使用它们来显示类型错误,当您尝试执行您现在正在执行的操作时。你为什么不想TheTabBarController是如果它实现了协议的UINavigationControllerDelegate

在Objective-C,你可以调用任何对象的任何方法,它可以通过实施forwardInvocation:(NSInvocation *)anInvocation或相关方法中的一种处理。您也可以在使用objc_install_instance_method和相关功能运行时添加新方法的对象或类。