Device Orientation vs. Interface Orientation

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.

Source code in Orientation.zip

  1. main.m
  2. Class OrientationAppDelegate
  3. Class ViewController
  4. Class View

Create the project

When creating class ViewController,
Choose a template for your new file: UIViewController subclass
Choose no options.

Class View is a sublcass of class UIView.

Things to try

  1. So what’s the difference between the device orientation and the interface orientation? One difference is that there are seven possible values for a 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.