为什么不需要在.h文件中声明此方法?

时间:2012-08-14 19:39:02

标签: iphone objective-c

为什么我可以在orientationChanged:文件中未声明的情况下访问.h?我在Apple的文档中找不到该方法,所以我不认为它是一种继承方法。

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

1 个答案:

答案 0 :(得分:4)

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