stumped
stumped

Reputation: 3293

Why doesn't this method need to be declared in the .h file?

Why am I allowed to access orientationChanged: without declaring it in the .h file? I couldn't find the method in Apple's documentations, so I don't think it's an inherited method.

#import <UIKit/UIKit.h>

@interface RotationAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

#import "RotationAppDelegate.h"

@implementation RotationAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UIDevice *device = [UIDevice currentDevice];
    [device beginGeneratingDeviceOrientationNotifications];
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:device];

    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

-(void)orientationChanged:(NSNotification *)note {
    NSLog(@"oriendtationChanged: %i", [[note object] orientation]);
}

Upvotes: 2

Views: 78

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726559

Methods that you pass to @selector(...) do not need to be declared in a header file. In fact, they do not even need to exist, as long as nobody tries to execute them at runtime. A string orientationChanged: is perfectly sufficient for the @selector(...) to produce a valid selector for you; as long as the corresponding method is available at run-time (i.e. the object's respondsToSelector: returns YES for it), the system will find and execute it correctly.

Upvotes: 4

Related Questions