【发布时间】:2017-10-17 11:44:00
【问题描述】:
我有一个 UITabBarController 作为根视图控制器。
在这个 UITabBarController 中,我有 2 个选项卡:tabA 和 tabB。
tabA 是通用视图控制器,tabB 是带有容器视图的视图控制器,其中嵌入了 pageViewcontroller C。
现在tabA中多了一个按钮,我想实现的效果是当我点击这个按钮时,它会跳转到tabB并显示C中的第二页,每次点击按钮时都会调用函数:
func swipeToIndex(ToIndex: Int, completion: (()->Void)? = nil) {
if self.currentPageIndex > 2 {
return
}
if ToIndex < self.currentPageIndex {
let toViewController = self.orderedViewControllers[ToIndex]
self.setViewControllers([toViewController], direction: .reverse, animated: true, completion: {finish in
self.currentPageIndex = ToIndex
completion?()
})
}
else if ToIndex > self.currentPageIndex {
let toViewController = self.orderedViewControllers[ToIndex]
self.setViewControllers([toViewController], direction: .forward, animated: true, completion: {finish in
self.currentPageIndex = ToIndex
completion?()
})
}
}
我只能在第二次点击按钮时意识到这一点。第一次,它转到 C 语言的第一页。我发现它与 viewDidLoad() 有关。当它在
之后第一次调用函数 swipeToIndex 时self.setViewControllers([toViewController], 方向: .forward, 动画: true, 完成: {finish in self.currentPageIndex = ToIndex 完成?() }) 它会调用 viewDidLoad,在里面再次设置 viewcontroller,如下所示:
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
我不知道第一次调用 swipeToIndex
时如何避免这种情况【问题讨论】:
-
因此,如果您打开应用程序,导航到 tabB(显示页面控制器),然后导航回 tabA,按下按钮会正常工作吗? (在至少看到一次 tabB 之前按下 tabA 上的按钮将不起作用)
-
@MaticOblak 是的,如果导航到 tabB 并返回 tabA,按下按钮将正常工作。但是我需要意识到即使没有看到tabB,点击tabA上的按钮也会起作用。
标签: ios swift uitabbarcontroller uipageviewcontroller