从 UITabBarController 中的另一个选项卡以编程方式在 UIPageViewController 中翻页
turn page programmatically in UIPageViewController from another tab in UITabBarController
我有一个 UITabBarController 作为根视图控制器。
在这个 UITabBarController 中,我有 2 个选项卡:tabA 和 tabB。
tabA 是一个通用视图控制器,而 tabB 是一个 viewController 带有容器视图的容器视图,其中嵌入了一个 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], direction: .forward, animated: true, completion: {finish in
self.currentPageIndex = ToIndex
completion?()
})
it will call viewDidLoad, inside there sets the viewcontroller again like following:
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
第一次调用 swipeToIndex
时,我不知道如何避免这种情况
您需要更智能地转发消息并检查是否已加载视图。
在您的页面控制器中,您应该检查是否已加载视图:
func swipeToIndex(ToIndex: Int, completion: (()->Void)? = nil) {
guard isViewLoaded else {
self.pageToSwipeTo = ToIndex
return
}
所以你需要添加
var pageToSwipeTo: Int? = nil
然后在viewDidLoad
最后尝试
if let pageToSwipeTo = pageToSwipeTo {
self.pageToSwipeTo = nil
self. swipeToIndex(pageToSwipeTo)
}
这是假设页面视图控制器在您的情况下已经存在。由于您的层次结构,这实际上可能不是真的,因此您可能需要甚至通过选项卡栏视图控制器转发消息...但请先尝试此过程。
我有一个 UITabBarController 作为根视图控制器。
在这个 UITabBarController 中,我有 2 个选项卡:tabA 和 tabB。
tabA 是一个通用视图控制器,而 tabB 是一个 viewController 带有容器视图的容器视图,其中嵌入了一个 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], direction: .forward, animated: true, completion: {finish in self.currentPageIndex = ToIndex completion?() }) it will call viewDidLoad, inside there sets the viewcontroller again like following:
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
第一次调用 swipeToIndex
时,我不知道如何避免这种情况您需要更智能地转发消息并检查是否已加载视图。
在您的页面控制器中,您应该检查是否已加载视图:
func swipeToIndex(ToIndex: Int, completion: (()->Void)? = nil) {
guard isViewLoaded else {
self.pageToSwipeTo = ToIndex
return
}
所以你需要添加
var pageToSwipeTo: Int? = nil
然后在viewDidLoad
最后尝试
if let pageToSwipeTo = pageToSwipeTo {
self.pageToSwipeTo = nil
self. swipeToIndex(pageToSwipeTo)
}
这是假设页面视图控制器在您的情况下已经存在。由于您的层次结构,这实际上可能不是真的,因此您可能需要甚至通过选项卡栏视图控制器转发消息...但请先尝试此过程。