Cocoa Bindings

Episode #152 | 13 minutes | published on January 15, 2015
Subscribers Only
In this episode I take a look at the basics of Cocoa Bindings. With Bindings you can have your controls on your view bound to properties on your controller or model, and even have controls bound to themselves. What would normally be a lot of manual plumbing code is handled for you automatically by bindings. To demonstrate, we build a live-updating temperature converter.

Episode Links

The Converter Object

+ (NSSet *)keyPathsForValuesAffectingCelsius {
    return [NSSet setWithObject:@"fahrenheit"];
}

- (CGFloat)celsius {
    return (self.fahrenheit - 32) / 1.8;
}

- (void)setCelsius:(CGFloat)celsius {
    self.fahrenheit = (celsius * 1.8) + 32;
}

- (void)setNilValueForKey:(NSString *)key {
    if ([key isEqualToString:@"celsius"] || [key isEqualToString:@"fahrenheit"]) {
        [self setValue:@0 forKey:key];
    } else {
        [super setNilValueForKey:key];
    }
}

@end
blog comments powered by Disqus