Bronze Challenge


#1

[code]//
// HeavyRotationAppDelegate.m
// HeavyRotation
//
// Created by Roy Law on 13-5-31.
// Copyright © 2013年 Roy Law. All rights reserved.
//

#import “HeavyRotationAppDelegate.h”
#import “HeavyRotationViewController.h”

@implementation HeavyRotationAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    UIDevice *myDevice = [UIDevice currentDevice];
    [myDevice beginGeneratingDeviceOrientationNotifications];
    myDevice.proximityMonitoringEnabled = YES;

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object];

    [nc addObserver:self selector:@selector(closeToFace:) name:UIDeviceProximityStateDidChangeNotification object];

    HeavyRotationViewController *hVC = [[HeavyRotationViewController alloc]init];
    self.window.rootViewController =hVC;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
    }

-(void)orientationChanged:(NSNotification *)note
{
NSLog(@“orientation changed as: %d”, [[note object] orientation]);
}

-(void)closeToFace:(NSNotification *)note
{
[self.window.rootViewController.view setBackgroundColor:[UIColor darkGrayColor]];
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
[/code]


#2

I see you’ve done it differently.

Would it not be clearer to have the observer being an instance of the HeavyViewController class?

That way, you could implement the change background colour within the controller, which would stick to the pattern that each controller controls its view.
In your case - that isn’t so?


#3

[quote=“Tander”]Would it not be clearer to have the observer being an instance of the HeavyViewController class?

That way, you could implement the change background colour within the controller, which would stick to the pattern that each controller controls its view.
In your case - that isn’t so?[/quote]

I agree with you.
The only reason the book sets self as observer for orientationChanged: is because it prints to the console. It’s not manipulating any views.
This time, the observer is called to manipulate one of its subview’s background color.

So here’s how I did it.

In RotationAppDelegate.m

[device setProximityMonitoringEnabled:YES]; [nc addObserver:hvc selector:@selector(proximityChanged:) name:UIDeviceProximityStateDidChangeNotification object:device];

Second, declaring the selector to be dispatched in case of notification in HeavyViewController.h

[code]#import <UIKit/UIKit.h>

@interface HeavyViewController : UIViewController
@property IBOutlet UIImageView *image;

  • (void)proximityChanged:(NSNotification *)notification;

@end
[/code]

Next, implement in HeavyViewController.m

@implementation HeavyViewController

Chapter 8 Bronze
 @synthesize image;
 - (void)proximityChanged:(NSNotification *)notification
 {
    [image setBackgroundColor:[UIColor darkGrayColor]];
 }
// This next code is in lieu of the deprecated method
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

@end

And lastly, I connected the outlet to UIImageView.