_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
登録
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(output:) name:@"MyEvent" object:nil];
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
セレクターメソッド(イベント処理)
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
- (void)output:(NSNotification *)notification
{
NSNumber *val = (NSNumber *)notification.object;
NSLog(@"%d", val.intValue);
}
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
通知(同期)
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
NSNumber *val = [NSNumber numberWithInt:1];
NSNotification *n = [NSNotification notificationWithName:@"MyEvent" object:val];
[[NSNotificationCenter defaultCenter] postNotification:n];
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
通知(非同期)
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
NSNumber *val = [NSNumber numberWithInt:1];
NSNotification *notification = [NSNotification notificationWithName:@"MyEvent" object:val];
NSNotificationQueue *queue = [NSNotificationQueue defaultQueue];
[queue enqueueNotification:notification postingStyle:NSPostNow/* NSPostWhenIdle */];
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
削除
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyEvent" object:nil];
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
通知を受け取ってブロックを実行する
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
self.localeChangeObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"x"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *notification) {
NSLog(@"%@", notification.name);
}];
queueに[NSOperationQueue mainQueue]を指定した場合はメインスレッドで、nilの場合は通知の送り主と同じスレッドでブロックが実行されます。
[[NSNotificationCenter defaultCenter] removeObserver:self.localeChangeObserver];
オブザーバをremoveするためにはローカルの変数などに戻り値を保持しておく必要があるので注意です。
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
デバイス回転通知
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
// デバイス回転通知登録
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(didReceiveDeviceOrientationNotification:)
name:UIDeviceOrientationDidChangeNotification object:nil];
- (void)dealloc
{
// デバイス回転通知解除
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
- (void)didReceiveDeviceOrientationNotification:(NSNotification *)notification
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIDeviceOrientation orientation = [[notification object] orientation];
NSLog(@"%@, %ld", NSStringFromCGRect(screenRect), (long)orientation);
switch (orientation) {
case UIDeviceOrientationPortrait: // 縦(ホームボタン下)
break;
case UIDeviceOrientationPortraitUpsideDown: // 縦(ホームボタン上)
break;
case UIDeviceOrientationLandscapeLeft: // 横(ホームボタン左)
break;
case UIDeviceOrientationLandscapeRight: // 横(ホームボタン右)
break;
case UIDeviceOrientationFaceUp: // 液晶面上(天)
break;
case UIDeviceOrientationFaceDown: // 液晶面下(地)
break;
case UIDeviceOrientationUnknown: // 向き不明
break;
default:
break;
}
}
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
アプリ通知
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
// アプリ通知設定
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];
/* 参考
アプリケーションがアクティブになった時に通知
UIApplicationDidBecomeActiveNotification
アプリケーションがバックグラウンドに入る時に通知
UIApplicationDidEnterBackgroundNotification
アプリケーションが起動した直後に通知
UIApplicationDidFinishLaunchingNotification
アプリケーションがアクティブになる直前に通知
UIApplicationWillEnterForegroundNotification
アプリケーションがアクティブで無くなる直前に通知
UIApplicationWillResignActiveNotification
アプリケーションが終了される直前に通知
UIApplicationWillTerminateNotification
ステータスバーのサイズ変更される直前に通知
UIApplicationWillChangeStatusBarOrientationNotification
デバイスの向きが変わった後に通知
UIApplicationDidChangeStatusBarOrientationNotification
*/
/**
* 解放時に呼ばれる
* @param 無し
* @return 無し
*/
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
}
/**
* アクティブ時に呼ばれる(オーバーライド用)
* @param 無し
* @return 無し
*/
- (void)applicationDidBecomeActive
{
}
/**
* アクティブで無くなる直前に呼ばれる(オーバーライド用)
* @param 無し
* @return 無し
*/
- (void)applicationWillResignActive
{
}
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
以下、参考用とする
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
#if TRUE
NSNotification *notification = [NSNotification notificationWithName:SAWNetworkManagerDidDownloadImageNotification object:bImageNo];
NSNotificationQueue *queue = [NSNotificationQueue defaultQueue];
[queue enqueueNotification:notification postingStyle:NSPostNow];
#else
[[NSNotificationCenter defaultCenter] postNotificationName:SAWNetworkManagerDidDownloadImageNotification
object:bImageNo];
#endif
最終更新:2017年04月03日 13:36