Skip to content

Instantly share code, notes, and snippets.

@willryanuk
Created October 26, 2012 11:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save willryanuk/3958325 to your computer and use it in GitHub Desktop.
Save willryanuk/3958325 to your computer and use it in GitHub Desktop.
This is a UISwitch with a block based hander for value change control events
//
// ZUISwitch.h
// zeebox
//
// Created by Will on 26/10/2012.
// Copyright (c) 2012 Electric Labs. All rights reserved.
//
@interface ZUISwitch : UISwitch
- (void)onValueChange:(void (^)(UISwitch *uiSwitch)) block;
@end
//
// ZUISwitch.m
// zeebox
//
// Created by Will on 26/10/2012.
// Copyright (c) 2012 zeebox. All rights reserved.
//
#import "ZUISwitch.h"
@interface ZUISwitch ()
@property (nonatomic, copy) void (^valueChangeBlock)(UISwitch *uiSwitch);
@end
@implementation ZUISwitch
- (void) awakeFromNib {
[self commonInit];
}
- (id)init {
self = [super init];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit {
[self addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
}
- (void)onValueChange:(void (^)(UISwitch *uiSwitch)) block {
self.valueChangeBlock = block;
}
- (void)switchValueChanged:(id)sender {
if(_valueChangeBlock) {
_valueChangeBlock(self);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment