Skip to content

Commit 4fe7247

Browse files
fix: deleting multiple views on iOS (#148)
1 parent e12c183 commit 4fe7247

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

ios/ReactNativePageView.m

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,13 @@ - (void)layoutSubviews {
231231
if (_reactPageViewController) {
232232
[self shouldScroll:_scrollEnabled];
233233
//Below line fix bug, where the view does not update after orientation changed.
234-
[self goTo:[NSNumber numberWithInteger:_currentIndex] animated:NO];
234+
[self goTo:@(_currentIndex) animated:NO];
235235
} else {
236236
[self embed];
237237
}
238238
}
239239

240-
- (void)didUpdateReactSubviews {
240+
- (void)didUpdateReactSubviews {
241241
if (_childrenViewControllers.count == 0){
242242
return;
243243
}
@@ -258,7 +258,7 @@ - (void)addPages {
258258
}
259259
_childrenViewControllers = tempChildrenViewControllers;
260260
_reactPageIndicatorView.numberOfPages = _childrenViewControllers.count;
261-
[self goTo:[NSNumber numberWithInteger:_currentIndex] animated:NO];
261+
[self goTo:@(_currentIndex) animated:NO];
262262

263263
} else {
264264
RCTLog(@"getParentViewController returns nil");
@@ -365,16 +365,16 @@ - (void)setReactViewControllers:(NSInteger)index
365365
__weak ReactNativePageView *weakSelf = self;
366366
uint16_t coalescingKey = _coalescingKey++;
367367
[_reactPageViewController
368-
setViewControllers:[NSArray arrayWithObjects:pageViewController, nil]
368+
setViewControllers:@[pageViewController]
369369
direction:direction
370370
animated:animated
371371
completion:^(BOOL finished) {
372-
weakSelf.currentIndex = index;
373-
if (weakSelf.eventDispatcher) {
374-
[weakSelf.eventDispatcher sendEvent:[[RCTOnPageSelected alloc] initWithReactTag:weakSelf.reactTag position:[NSNumber numberWithInteger:index] coalescingKey:coalescingKey]];
375-
}
376-
377-
}];
372+
weakSelf.currentIndex = index;
373+
if (weakSelf.eventDispatcher) {
374+
[weakSelf.eventDispatcher sendEvent:[[RCTOnPageSelected alloc] initWithReactTag:weakSelf.reactTag position:[NSNumber numberWithInteger:index] coalescingKey:coalescingKey]];
375+
}
376+
377+
}];
378378
}
379379

380380
- (UIViewController *)createChildViewController:(UIView *)view {
@@ -384,22 +384,21 @@ - (UIViewController *)createChildViewController:(UIView *)view {
384384
}
385385

386386
- (void)goTo:(NSNumber *)index animated:(BOOL)animated {
387-
if (_currentIndex >= 0 &&
388-
index.integerValue < _childrenViewControllers.count) {
389-
390-
_reactPageIndicatorView.currentPage = index.integerValue;
387+
if (_currentIndex >= 0) {
391388
UIPageViewControllerNavigationDirection direction =
392389
(index.integerValue > _currentIndex)
393390
? UIPageViewControllerNavigationDirectionForward
394391
: UIPageViewControllerNavigationDirectionReverse;
395392

396-
UIViewController *viewController =
397-
[_childrenViewControllers objectAtIndex:index.integerValue];
398-
[self setReactViewControllers:index.integerValue
399-
with:viewController
393+
394+
NSInteger indexToDisplay = index.integerValue < _childrenViewControllers.count ? index.integerValue : _childrenViewControllers.count - 1;
395+
UIViewController *controllerToDisplay = _childrenViewControllers[indexToDisplay];
396+
_reactPageIndicatorView.currentPage = indexToDisplay;
397+
398+
[self setReactViewControllers:indexToDisplay
399+
with:controllerToDisplay
400400
direction:direction
401401
animated:animated];
402-
403402
}
404403
}
405404

@@ -478,8 +477,6 @@ - (void)shouldShowPageIndicator:(BOOL)showPageIndicator {
478477
}
479478

480479
- (UIPageControl *)createPageIndicator:(UIView *)parentView {
481-
CGPoint parentOrigin = parentView.frame.origin;
482-
CGSize parentSize = parentView.frame.size;
483480
UIPageControl *pageControl = [[UIPageControl alloc] init];
484481
pageControl.numberOfPages = _childrenViewControllers.count;
485482
pageControl.currentPage = _initialPage;
@@ -522,4 +519,3 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
522519
}
523520

524521
@end
525-

0 commit comments

Comments
 (0)