Skip to content

Commit 292eee5

Browse files
authored
fix(ios): load initial tab when onSwitchToTab mode is set. (#7924)
Setting the initial tab index before the initialization doesn't work, since it resets to the default value (0) after initializing the tab bar controller. It also seems that it loads the default child from index 0 regardless of setting it a default. I tried to workaround this from different steps in the lifecycle of the controller but looks like there's a native issue there.
1 parent 73a8564 commit 292eee5

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

lib/ios/RNNBottomTabsController.m

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,21 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
2929
_bottomTabPresenter = bottomTabPresenter;
3030
_dotIndicatorPresenter = dotIndicatorPresenter;
3131

32-
if ([options.bottomTabs.currentTabIndex hasValue]) {
33-
_currentTabIndex = [options.bottomTabs.currentTabIndex get];
34-
_previousTabIndex = _currentTabIndex;
32+
self = [super initWithLayoutInfo:layoutInfo
33+
creator:creator
34+
options:options
35+
defaultOptions:defaultOptions
36+
presenter:presenter
37+
eventEmitter:eventEmitter
38+
childViewControllers:childViewControllers];
39+
40+
IntNumber *currentTabIndex = options.bottomTabs.currentTabIndex;
41+
if ([currentTabIndex hasValue]) {
42+
NSUInteger currentTabIndexValue = [currentTabIndex get];
43+
_previousTabIndex = currentTabIndexValue;
44+
_currentTabIndex = currentTabIndexValue;
3545
}
3646

37-
self = [super initWithLayoutInfo:layoutInfo
38-
creator:creator
39-
options:options
40-
defaultOptions:defaultOptions
41-
presenter:presenter
42-
eventEmitter:eventEmitter
43-
childViewControllers:childViewControllers];
4447
if (@available(iOS 13.0, *)) {
4548
self.tabBar.standardAppearance = [UITabBarAppearance new];
4649
}

playground/ios/NavigationTests/RNNCommandsHandlerTest.m

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,10 @@ - (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTab {
515515
commandId:@""
516516
completion:^(NSString *componentId){
517517
}];
518+
518519
XCTAssertTrue(_vc1.isViewLoaded);
519520
XCTAssertFalse(_vc2.isViewLoaded);
521+
520522
[tabBarController setSelectedIndex:1];
521523
XCTAssertTrue(_vc2.isViewLoaded);
522524
}
@@ -531,6 +533,7 @@ - (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTabWithCustomIndex {
531533

532534
BottomTabsBaseAttacher *attacher =
533535
[[[BottomTabsAttachModeFactory alloc] initWithDefaultOptions:nil] fromOptions:options];
536+
534537
RNNBottomTabsController *tabBarController =
535538
[[RNNBottomTabsController alloc] initWithLayoutInfo:nil
536539
creator:nil
@@ -540,19 +543,31 @@ - (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTabWithCustomIndex {
540543
bottomTabPresenter:nil
541544
dotIndicatorPresenter:nil
542545
eventEmitter:_eventEmmiter
543-
childViewControllers:@[ _vc1, _vc2 ]
546+
childViewControllers:@[ _vc1, _vc2, _vc3 ]
544547
bottomTabsAttacher:attacher];
548+
545549
[tabBarController viewWillAppear:YES];
546550
OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(tabBarController);
547551

548552
[self.uut setRoot:@{}
549553
commandId:@""
550554
completion:^(NSString *componentId){
551555
}];
552-
XCTAssertFalse(_vc1.isViewLoaded);
553-
XCTAssertTrue(_vc2.isViewLoaded);
554-
[tabBarController setSelectedIndex:0];
555-
XCTAssertTrue(_vc1.isViewLoaded);
556+
557+
// TODO: for some reason the controller always loads the default controller (index 0), regardless the initial value.
558+
XCTAssertTrue(_vc1.isViewLoaded);
559+
XCTAssertTrue(_vc2.isViewLoaded);
560+
XCTAssertFalse(_vc3.isViewLoaded);
561+
562+
[tabBarController setSelectedIndex:0];
563+
XCTAssertTrue(_vc1.isViewLoaded);
564+
XCTAssertTrue(_vc2.isViewLoaded);
565+
XCTAssertFalse(_vc3.isViewLoaded);
566+
567+
[tabBarController setSelectedIndex:2];
568+
XCTAssertTrue(_vc1.isViewLoaded);
569+
XCTAssertTrue(_vc2.isViewLoaded);
570+
XCTAssertTrue(_vc3.isViewLoaded);
556571
}
557572

558573
- (void)testSetRoot_withBottomTabsAttachModeAfterInitialTab {

0 commit comments

Comments
 (0)