//
// ViewController.m
// NSNotification
//
// Created by Liming Tian on 11/25/14.
// Copyright (c) 2014 Liming Tian. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
- (void)posterNotificationWithString:(NSString *)orientation;
- (void)useNotificationWithString:(NSNotification *)notification;
@end
static NSString *notificationName = @"MTPosterNotificationTut";
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(useNotificationWithString:)
name:notificationName
object:nil];
}
- (void)posterNotificationWithString:(NSString *)orientation
{
NSString *key = @"orientationValue";
NSDictionary *dic = [NSDictionary dictionaryWithObject:orientation forKey:key];
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:dic];
}
- (void)useNotificationWithString:(NSNotification *)notification
{
NSString *key = @"orientationValue";
NSDictionary *dic = notification.userInfo;
NSString *stringKeyToUse = [dic objectForKey:key];
NSLog(@"device orientation is:%@",stringKeyToUse);
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"%s",__FUNCTION__);
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
[self posterNotificationWithString:@"LandscapeLeft"];
} else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[self posterNotificationWithString:@"LandscapeRight"];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end