Call to willAnimateRotationToInterfaceOrientation:duration:


#1

Regarding willAnimateRotationToInterfaceOrientation:duration:, the “For the More Curious” section in Chapter 8 states that:

I’m even more curious about how this actually happens. Does code in the UIViewController superclass actually check to see that the subclass has overridden this method prior to sending the message? If so, does this imply that there’s a way to introspect subclasses? If so, is that something that’s part of NSObject like respondsToSelector: or is it something that’s part of the Objective-C runtime, like class_copyMethodList? Or, alternatively, does the method simply not exist in the UIViewController superclass at all, such that it’s only present if implemented by the subclass?

The documentation for willAnimateRotationToInterfaceOrientation:duration: seems to imply that the method does exist at UIViewController and that it’s called unconditionally by the UIViewController. To me this implies that UIViewController provides a default implementation, and that subclasses can override the default do-nothing implementation. The documentation for respondsToSelector: describes how to look “up” the inheritance chain, but not down.

As you can probably tell, I’m new to Objective-C, so understanding how this works would be pretty helpful as I get my head around the language and runtime.

Thanks!


#2

A class does not know that there are subclasses of it. It’s the caller’s responsibility to make sure the receiver object responds to a message to avoid runtime errors.


#3

Great, thank you for making that clear. This makes sense based on other languages I’ve worked with and is how I expected Objective-C to work.

Here’s where I’m confused.

[ul]
[li]According to the Documentation and API Reference in Xcode, UIViewController defines willAnimateRotationToInterfaceOrientation:duration:.

[/li]
[li]My view controller is a subclass of UIViewController. If I implement willAnimateRotationToInterfaceOrientation:duration in my view controller, I therefore override the superclass implementation.

[/li]
[li]The book states that “When a view controller is about to autorotate its view, it checks to see if you have implemented this method. If you have, then this method is invoked during the animation block of the rotation code.”

[/li]
[li]Per the above, “A class does not know that there are subclasses of it. It’s the caller’s responsibility to make sure the receiver object responds to a message to avoid runtime errors.”[/li][/ul]

If it’s the caller’s responsibility to make sure the receiver object responds, and the book states that the view controller makes the check, but the implementer is a subclass of the view controller, how does that happen?

Thanks again for your help.


#4

I think maybe I’m making my question more complex than it needs to be. At the end of chapter 8, the book says:

When I subclass UIViewController and implement willAnimateRotationToInterfaceOrientation:duration:, how does the view controller check to see that I’ve implemented this method so that it can call it during the animation block?