Skip to content

Instantly share code, notes, and snippets.

@Morse-Code
Forked from irace/gist:5272146
Last active December 15, 2015 20:28
Show Gist options
  • Save Morse-Code/5318483 to your computer and use it in GitHub Desktop.
Save Morse-Code/5318483 to your computer and use it in GitHub Desktop.
Custom UISwitch subclass to control switch state programmatically.
@interface UntoggleableSwitch : UISwitch
@end
#import "UntoggleableSwitch.h"
@interface ProxyControl : UIControl
@property (nonatomic, weak) UIControl *actualControl;
@end
@interface UntoggleableSwitch()
@property (nonatomic, strong) ProxyControl *proxyControl;
@end
@implementation UntoggleableSwitch
#pragma mark - NSObject
- (id)init {
if (self = [super init]) {
self.proxyControl = [[ProxyControl alloc] initWithFrame:self.frame];
self.proxyControl.actualControl = self;
[self addSubview:self.proxyControl];
}
return self;
}
#pragma mark - UIControl
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents {
[self.proxyControl addTarget:target action:action forControlEvents:controlEvents];
}
@end
@implementation ProxyControl
#pragma mark - UIControl
- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
UIControl *actualControl = self.actualControl;
[actualControl sendAction:action to:target forEvent:event];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment