2012-08-14 85 views
2

为什么我被允许访问orientationChanged:而未声明它在.h文件中?我无法在Apple的文档中找到该方法,所以我不认为它是一种继承方法。您传递给@selector(...)为什么不需要在.h文件中声明此方法?

#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]); 
} 

回答

4

方法不需要在头文件中声明。事实上,只要没有人试图在运行时执行它们,它们甚至不需要存在。字符串orientationChanged:对于@selector(...)为您生成一个有效的选择器已经足够了;只要相应的方法在运行时可用(即对象的respondsToSelector:返回YES),系统将正确地找到并执行它。

相关问题