The application delegate creates the view controller, and the view controller creates the view. Every view controller contains a pointer down to its view. Our view contains a pointer up to the view controller that watches over it. The view uses this pointer to get the interface orientation and the device orientation from the view controller.
An object can have at most delegate,
but the
notification
center
can notify many other objects when something happens.
In this app, the notification center
notifies the view controller when the device orientation changes.
When this happens,
the view controller sends the
setNeedsDisplay
message to the view,
which triggers a call to the view’s
drawRect:
method.
Even without the notification center,
the view controller would still call the
drawRect:
method of the view whenever the interface orientation changes.
main.m
OrientationAppDelegate
ViewController
View
When creating class
ViewController
,
Choose a template for your new file: UIViewController
subclass
Choose no options.
Class
View
is a sublcass of class
UIView
.
UIDeviceOrientation
but only four for a
UIInterfaceOrientation
.
To see the real difference,
uncomment the
return (interfaceOrientation == UIInterfaceOrientationPortrait);in the
shouldAutorotateToInterfaceOrientation:
method of the view controller.