Skip to content

Commit 104e9f8

Browse files
Fix responder chain in reactViewController on macOS (#2380)
## Summary: I first noticed this while working on SwiftUI views for expo/expo#35078. Calling `reactViewController` on iOS always returns the correct view controller but on macOS, this same function will return nil for nested views. So this PR addresses the differences in responder chain behavior between iOS and macOS when aliasing UIViewController to NSViewController. On iOS, a view's nextResponder is naturally its own UIViewController, ensuring that helper functions like reactViewController work as expected. However, on macOS, an NSView's nextResponder does not include its NSViewController by default, which causes issues when trying to retrieve the view controller directly from the responder chain. This change ensures that traversals will properly locate the NSViewController using the superview chain, mirroring iOS behavior. ## Test Plan: Run RNTester and we've been using this same function to render SwiftUI views on macOS in expo's repo Co-authored-by: Saad Najmi <[email protected]>
1 parent 47880ca commit 104e9f8

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

packages/react-native/React/Views/UIView+React.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,24 @@ static void updateTransform(RCTPlatformView *view) // [macOS]
313313

314314
- (UIViewController *)reactViewController
315315
{
316+
#if !TARGET_OS_OSX // [macOS]
316317
id responder = [self nextResponder];
317318
while (responder) {
318319
if ([responder isKindOfClass:[UIViewController class]]) {
319320
return responder;
320321
}
321322
responder = [responder nextResponder];
322323
}
324+
#else // [macOS
325+
NSView *view = self;
326+
while (view) {
327+
if ([view.nextResponder isKindOfClass:[NSViewController class]]) {
328+
return (NSViewController *)view.nextResponder;
329+
}
330+
view = view.superview;
331+
}
332+
#endif // macOS]
333+
323334
return nil;
324335
}
325336

0 commit comments

Comments
 (0)