Skip to content

Instantly share code, notes, and snippets.

@maxsz
Created May 4, 2016 09:32
Show Gist options
  • Save maxsz/a27b0c8c7d318166a9588dc4e55e4450 to your computer and use it in GitHub Desktop.
Save maxsz/a27b0c8c7d318166a9588dc4e55e4450 to your computer and use it in GitHub Desktop.
Workaround for firstResponder NSPopover bug
- (void)popoverWillClose:(NSNotification *)notification
{
// https://openradar.appspot.com/26064913
// Turns out that Apple has a bug in their responder chain. It stores a non-weak (assign)
// "_previousResponder", which is a button inside the deallocated popover. During window
// state restoration/archiving, it tries to access this object (the button) and crashes.
// We work around this by resetting the "_previousResponder" when the popover closes.
NSResponder *firstResponder = [[[self view] window] firstResponder];
[[[self view] window] makeFirstResponder:nil];
if ([[[_popover contentViewController] view] window] == firstResponder) {
return;
}
if ([firstResponder isKindOfClass:[NSView class]]) {
NSView *responderView = (NSView *)firstResponder;
while (responderView) {
if ([responderView superview] == [[_popover contentViewController] view]) {
return;
}
responderView = [responderView superview];
}
}
[[[self view] window] makeFirstResponder:firstResponder];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment