Skip to content

Instantly share code, notes, and snippets.

@KosmicTask
Created August 23, 2016 14:50
Show Gist options
  • Save KosmicTask/64b0cf2f51fad04dd26ef7e469424645 to your computer and use it in GitHub Desktop.
Save KosmicTask/64b0cf2f51fad04dd26ef7e469424645 to your computer and use it in GitHub Desktop.
Log NSWindow key view loop starting at the first responder.
@interface NSWindow (KVLoop)
- (IBAction)logKeyViewLoop:(id)sender;
@end
#include "NSWindow+KVLoop.h"
@implementation NSWindow (KVLoop)
- (IBAction)logKeyViewLoop:(id)sender
{
[self recalculateKeyViewLoop];
if (!self.firstResponder) {
NSLog(@"Cannot log key view loop : no first responder found.");
return;
}
if (![self.firstResponder isKindOfClass:[NSView class]]) {
NSLog(@"Cannot log key view loop : first responder is not a view.");
return;
}
NSView *view = (NSView *)self.firstResponder;
NSMutableArray *visitedViews = [NSMutableArray new];
do {
// handle the field editor
if ([view isKindOfClass:[NSTextView class]]) {
NSTextView *textView = (NSTextView *)view;
if (textView.isFieldEditor) {
view = (id)textView.delegate;
}
}
// log the next key view
NSView *nextView = view.nextValidKeyView; // or to show all views view.nextKeyView;
NSString *content = [view respondsToSelector:@selector(stringValue)] ? [(id)view stringValue] : @"?";
NSString *nextContent = [nextView respondsToSelector:@selector(stringValue)] ? [(id)nextView stringValue] : @"?";
NSString *canBecomeKeyView = [nextView acceptsFirstResponder] ? @"Y" : @"n";
NSLog(@"%@ %@ %@ : Next key view: %@ - %@", canBecomeKeyView, view.className, content, nextView.className, nextContent);
if (view) {
[visitedViews addObject:view];
}
view = nextView;
} while (view && ![visitedViews containsObject:view]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment