近来学习iOS开发,在研究通知中心的机制(notification center)时,编写了如下代码进行尝试,首先自定义一个类:
@interface Test1NotificationMy1 : NSObject
+(void) orientationChange:(NSNotification *)note;
-(void) orientationChangeInstance:(NSNotification *)note;
@end
@implementation Test1NotificationMy1
+(void) orientationChange:(NSNotification *)note
{
NSLog(@"类方法:方向变化了:%d", [[note object] orientation]);
}
-(void) orientationChangeInstance:(NSNotification *)note
{
NSLog(@"实例方法:方向变化了:%d", [[note object] orientation]);
}
然后在AppDelegate类里注册UIDeviceOrientationDidChangeNotification事件:
@implementation Test1NotificationAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customiz

这篇博客探讨了在Objective-C中如何在类的外部声明全局变量,并以使用UIDeviceOrientationDidChangeNotification事件为例,说明如何注册事件并在整个应用生命周期内响应设备方向变化。通过将对象作为外部变量声明,使得类的实例方法也能被调用,从而实现控制台在设备旋转时输出相关信息的功能。

3870

被折叠的 条评论
为什么被折叠?



