Skip to content

Commit 022c5cd

Browse files
committed
fix(ios): avoid setting selected-index before setup has finished.
This fixes the issue mentioned on #7921, #7922 and #7924. Changing the `selectedIndex` will not have any effect before the setup has finished, the original issue was the method `setSelectedIndex` was called immediately when calling to the `super` initializer with a `0` value regardless of the initial `currentTab`. This prevents the loading of the tab from index `0` regardless of the custom initial rendered tab.
1 parent 292eee5 commit 022c5cd

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

lib/ios/RNNBottomTabsController.m

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ @implementation RNNBottomTabsController {
1313
NSUInteger _previousTabIndex;
1414
BottomTabsBaseAttacher *_bottomTabsAttacher;
1515
BOOL _tabBarNeedsRestore;
16+
RNNNavigationOptions *_options;
17+
BOOL _didFinishSetup;
1618
}
1719

1820
- (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
@@ -28,6 +30,15 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
2830
_bottomTabsAttacher = bottomTabsAttacher;
2931
_bottomTabPresenter = bottomTabPresenter;
3032
_dotIndicatorPresenter = dotIndicatorPresenter;
33+
_options = options;
34+
_didFinishSetup = NO;
35+
36+
IntNumber *currentTabIndex = options.bottomTabs.currentTabIndex;
37+
if ([currentTabIndex hasValue]) {
38+
NSUInteger currentTabIndexValue = [currentTabIndex get];
39+
_previousTabIndex = currentTabIndexValue;
40+
_currentTabIndex = currentTabIndexValue;
41+
}
3142

3243
self = [super initWithLayoutInfo:layoutInfo
3344
creator:creator
@@ -37,13 +48,6 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
3748
eventEmitter:eventEmitter
3849
childViewControllers:childViewControllers];
3950

40-
IntNumber *currentTabIndex = options.bottomTabs.currentTabIndex;
41-
if ([currentTabIndex hasValue]) {
42-
NSUInteger currentTabIndexValue = [currentTabIndex get];
43-
_previousTabIndex = currentTabIndexValue;
44-
_currentTabIndex = currentTabIndexValue;
45-
}
46-
4751
if (@available(iOS 13.0, *)) {
4852
self.tabBar.standardAppearance = [UITabBarAppearance new];
4953
}
@@ -60,6 +64,7 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
6064
action:@selector(handleLongPressGesture:)];
6165
[self.tabBar addGestureRecognizer:self.longPressRecognizer];
6266

67+
_didFinishSetup = YES;
6368
return self;
6469
}
6570

@@ -132,8 +137,15 @@ - (void)setSelectedIndexByComponentID:(NSString *)componentID {
132137
}
133138

134139
- (void)setSelectedIndex:(NSUInteger)selectedIndex {
135-
_currentTabIndex = selectedIndex;
136-
[super setSelectedIndex:selectedIndex];
140+
IntNumber *currentTabIndex = _options.bottomTabs.currentTabIndex;
141+
if ([currentTabIndex hasValue] && !_didFinishSetup) {
142+
NSUInteger currentTabIndexValue = [currentTabIndex get];
143+
_currentTabIndex = currentTabIndexValue;
144+
} else {
145+
_currentTabIndex = selectedIndex;
146+
}
147+
148+
[super setSelectedIndex:_currentTabIndex];
137149
}
138150

139151
- (UIViewController *)selectedViewController {

playground/ios/NavigationTests/RNNCommandsHandlerTest.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,7 @@ - (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTabWithCustomIndex {
554554
completion:^(NSString *componentId){
555555
}];
556556

557-
// TODO: for some reason the controller always loads the default controller (index 0), regardless the initial value.
558-
XCTAssertTrue(_vc1.isViewLoaded);
557+
XCTAssertFalse(_vc1.isViewLoaded);
559558
XCTAssertTrue(_vc2.isViewLoaded);
560559
XCTAssertFalse(_vc3.isViewLoaded);
561560

0 commit comments

Comments
 (0)