Skip to content

Instantly share code, notes, and snippets.

@vincentlg
Created May 27, 2011 17:32
Show Gist options
  • Save vincentlg/995736 to your computer and use it in GitHub Desktop.
Save vincentlg/995736 to your computer and use it in GitHub Desktop.
//
// ClockView.m
// Clock
//
// Created by Vincent Le Gallic on 27/05/11.
// Copyright 2011 Frianbiz. All rights reserved.
//
#import "ClockView.h"
@implementation ClockView // Hérite de UILabel
@dynamic controller;
-(id)controller
{
return controller;
}
-(void)setController:(id)newController
{
if(controller != newController)
{
[controller removeObserver:self forKeyPath:@"model.second"];
[controller release];
controller = [newController retain];
//On ajoute un observer à notre objet qui sera déclenché quand la propriété model.second changera
[newController addObserver:self forKeyPath:@"model.second" options:NSKeyValueObservingOptionNew context:NULL];
}
}
//Sorte d'écouteur global appelé par tous les observer attaché à l'objet
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if(object == controller && [keyPath isEqualToString:@"model.second"]) //Appelée lorsque la propriété model.second change
{
NSNumber *hour= [object valueForKeyPath:@"model.hour"];
NSNumber *minute= [object valueForKeyPath:@"model.minute"];
NSNumber *second= [object valueForKeyPath:@"model.second"];
self.text = [NSString stringWithFormat:@"%02d:%02d:%02d",[hour intValue],[minute intValue],[second intValue]];
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)dealloc
{
self.controller = nil;
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment