In my TheTabBarController, without conforming to UINavigationControllerDelegate protocol, i could assign my class to moreNavigationController.delegate.
// without conforming to protocol, <UINavigationControllerDelegate>
@interface TheTabBarController : UITabBarController
self.moreNavigationController.delegate = self;
It just raises the following warning but compiles successfully.
Assigning to ‘id’ from incompatible
type ‘TheTabBarController *const __strong’
The protocol’s method is called at run-time without any error. I use it to hide more navigation bar for some view controllers.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
So, i want to know, is this legal and safe or; will it crash later or leak memory? How could this be allowed semantically? How could run-time resolves the method although its not defined in the interface and the protocol is not conformed? Or, UITabBarController uses a hidden category that conforms to the protocol?
Protocols have no runtime meaning. They are only used during compilation to display type errors when you try to do things like you are doing now. Why would you not want
TheTabBarControllerto be aUINavigationControllerDelegateif it implements the protocol?In Objective-C, you can call any method on any object and it might handle it by implementing
forwardInvocation:(NSInvocation *)anInvocationor one of the related methods. You can also add new methods to an object or class at runtime usingobjc_install_instance_methodand related functions.