2

为什么我允许访问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]);
}
4

1 回答 1

4

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

于 2012-08-14T19:45:11.953 回答