Latest 1.2.0
Homepage https://github.com/pompopo/ObserverKit
License MIT
Platforms ios 7.0, requires ARC
Authors

ObserverKit build pod license platform

A simple library to use UIControl, NSNotification, Key Value Observing…

Install

with CocoaPod

pod 'ObserverKit'

and

#import "OKObserver.h"
#import "NSObject+OKObserver.h" // Optional

Example


self.ok_observer.control(self.field1, UIControlEventEditingChanged, ^(typeof (self) me, UITextField *field) {
    me.i = [field.text integerValue];

}).control(self.field2, UIControlEventEditingChanged, ^(typeof(self)me, UITextField *field) {
    me.j = [field.text integerValue];

}).keyPath(@[@"i", @"j"], ^(typeof(self)me, id newValue, id oldValue, NSString *path) {
    NSLog(@"%@: %@ -> %@", path, oldValue, newValue);
    me.total = me.i + me.j;

}).keyPath(@"total", ^(typeof(self)me, id val){
    me.resultLabel.text = [NSString stringWithFormat:@"%ld", [val integerValue]];

}).notification(UIApplicationDidReceiveMemoryWarningNotification, ^{
    NSLog(@"Did receive memory warning");
});

Key Value Observing

// single key
self.ok_observer.keyPath(@"i", ^(typeof(self)me, id newValue, id oldValue) {
    NSLog(@"%@ -> %@", newValue, oldValue);
});

// multiple keys
self.ok_observer.keyPath(@[@"i", @"j"], ^(typeof(self)me, id newValue, id oldValue, NSString *keyPath) {
    if ([keyPath isEqualToString:@"i"]) {
        NSLog(@"i changed");
    } else {
        NSLog(@"j changed");
    }
});

// cast NSNumber, NSValue
self.ok_observer.keyPath(@"i", ^(typeof(self)me, NSInteger newValue) {
    // You don't have to write [newValue integerValue]
    if (newValue == 100) {
        ...
    }
});

UIControl

// single control
self.ok_observer.control(self.button, UIControlEventTouchUpInside, ^(typeof(self)me, UIButton *button, UIEvent *event) {
    NSLog(@"button touched");
});

// multiple controls
self.ok_observer.control(@[self.buttonA, self.buttonB], UIControlEventTouchUpInside, ^(typeof(self)me, UIButton *button) {
    if (button.tag == 1) {
        NSLog(@"buttonA touched");
    } else {
        NSLog(@"buttonB touched");
    }
});

// You can omit UIControlEvents with control2
self.ok_observer.control2(self.button, ^(typeof(self)me, UIButton *button, UIEvent *event) {
    NSLog(@"button touched");
});

NSNotification

// single notification
self.ok_observer.notification(UIApplicationDidReceiveMemoryWarningNotification, ^(id me, NSNotification *notification) {
    NSLog(@"memory warning");
});

// multiple notifications
self.ok_observer.notification(@[UIKeyboardDidShowNotification, UIKeyboardDidHideNotification], ^{
    NSLog(@"Keyboard did show or hide");
});

dealloc

You must stop observing in dealloc,

- (void)dealloc {
  [self ok_stop];
}

License

MIT License. See LICENSE.

Latest podspec

{
    "name": "ObserverKit",
    "version": "1.2.0",
    "summary": "Simple library for UIControl, NSNotification, Key Value Observing.",
    "homepage": "https://github.com/pompopo/ObserverKit",
    "license": "MIT",
    "authors": {
        "pompopo": "[email protected]"
    },
    "source": {
        "git": "https://github.com/pompopo/ObserverKit.git",
        "tag": "1.2.0"
    },
    "platforms": {
        "ios": "7.0"
    },
    "source_files": [
        "ObserverKit",
        "ObserverKit/*.{h,m}"
    ],
    "exclude_files": "ObserverKitDemo",
    "requires_arc": true
}

Pin It on Pinterest

Share This