Created
May 23, 2016 11:20
-
-
Save robbdimitrov/caa74c5354ef3c5bc02d21ad78f5564a to your computer and use it in GitHub Desktop.
Find the active responder by going through the responder chain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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