UIPageViewController:返回當前可見視圖你怎么知道里面顯示的當前頁面/視圖是UIPageViewController什么?我已經覆蓋了viewDidAppear我的子視圖的方法,因此他們在viewDidAppear方法中向父視圖發送了一個id 。但問題是:我無法可靠地將該id用作顯示頁面的id。因為如果用戶轉動頁面但中途決定停止轉動并將頁面放回,viewDidAppear則已經被調用。(視圖在卷曲頁面后面可見)。也許我應該只在當前視圖消失的情況下切換到新的id。但我想知道是否有更簡單的方法來返回當前可見的視圖?
3 回答

繁星coding
TA貢獻1797條經驗 獲得超4個贊
您應該手動跟蹤當前頁面。委托方法pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:
將告訴您何時更新該變量。該方法的最后一個參數transitionCompleted:
可以告訴您用戶是否完成了翻頁過渡。

一只斗牛犬
TA貢獻1784條經驗 獲得超2個贊
從iOS 6開始,我發現viewControllers
UIPageViewController 的屬性不斷更新,因此它將始終保存一個代表當前頁面的視圖控制器,而不是其他內容。因此,您可以通過調用viewControllers[0]
(假設您一次只顯示一個視圖控制器)來訪問當前頁面。
只有在頁面“鎖定”到位時,viewController數組才會更新,因此如果用戶決定部分顯示下一頁,則除非他們完成轉換,否則它不會成為“當前”頁面。
如果要跟蹤“頁碼”,請在通過UIPageViewController數據源方法創建視圖控制器時為其分配索引值。
例如:
-(void)autoAdvance { UIViewController *currentVC = self.viewControllers[0]; NSUInteger currentIndex = [myViewControllers indexOfObject:currentVC]; if ( currentIndex >= (myViewControllers.count-1) ) return; [self setViewControllers:@[myViewControllers[ currentIndex+1 ]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; }-(NSInteger)presentationIndexForPageViewController: (UIPageViewController *)pageViewController { // return 0; UIViewController *currentVC = self.viewControllers[0]; NSUInteger currentIndex = [myViewControllers indexOfObject:currentVC]; return currentIndex; }
但請注意這是不可靠的評論。

守著一只汪
TA貢獻1872條經驗 獲得超4個贊
不幸的是,以上所有方法都沒有幫助我。不過,我通過使用標簽找到了解決方案。可能它不是最好的,但它有效,并希望它可以幫助某人:
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed { if (completed) { int currentIndex = ((UIViewController *)self.pageViewController.viewControllers.firstObject).view.tag; self.pageControl.currentPage = currentIndex; }}
在Swift :(感謝@Jessy)
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool){ guard completed else { return } self.pageControl.currentPage = pageViewController.viewControllers!.first!.view.tag}
示例: gist
- 3 回答
- 0 關注
- 1212 瀏覽
添加回答
舉報
0/150
提交
取消