Skip to content

Instantly share code, notes, and snippets.

@robbdimitrov
Created May 23, 2016 11:20
Show Gist options
  • Save robbdimitrov/caa74c5354ef3c5bc02d21ad78f5564a to your computer and use it in GitHub Desktop.
Save robbdimitrov/caa74c5354ef3c5bc02d21ad78f5564a to your computer and use it in GitHub Desktop.
Find the active responder by going through the responder chain
//
// UIResponder+CMAdditions.h
// ProtoSketch
//
// Created by Robert Dimitrov on 5/23/16.
// Copyright © 2016 Codemotion Ltd. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIResponder (CMAdditions)
- (instancetype)cm_activeResponder;
@end
//
// UIResponder+CMAdditions.m
// ProtoSketch
//
// Created by Robert Dimitrov on 5/23/16.
// Copyright © 2016 Codemotion Ltd. All rights reserved.
//
#import "UIResponder+CMAdditions.h"
@implementation UIResponder (CMAdditions)
- (instancetype)cm_activeResponder {
UIResponder *activeResponder = nil;
if (self.isFirstResponder) {
activeResponder = self;
} else if ([self isKindOfClass:[UIViewController class]]) {
if ([(UIViewController *)self parentViewController]) {
activeResponder = [[(UIViewController *)self parentViewController] cm_activeResponder];
}
if (!activeResponder) {
activeResponder = [[(UIViewController *)self view] cm_activeResponder];
}
} else if ([self isKindOfClass:[UIView class]]) {
for (UIView *subview in [(UIView *)self subviews]) {
activeResponder = [subview cm_activeResponder];
if (activeResponder) break;
}
}
return activeResponder;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment