[英]Switch View Controllers in a Page View Controller Programmatically | Swift
我有一個UIButton
我(也創造編程) View Controller
這是在Page View Controller
。 我想切換到另一個View Controller
在同一頁面視圖控制器。 這是我嘗試使用的方法:
@objc func myOpinionAction(_ sender:UIButton!)
{
PageViewController().setViewControllers([suggestionPage()], direction: UIPageViewController.NavigationDirection.forward, animated: true)
}
這很明顯是行不通的(我正在嘗試切換到RecommendationionPage)我嘗試過的另一種方法是:
PageViewController().transition(from: PageViewController().viewControllers![0], to: PageViewController().viewControllers![2], duration: 0.3, options: UIView.AnimationOptions.curveEaseInOut, animations: nil, completion: nil)
這給了我一個線程1:我的信號SIGABRT錯誤:
class AppDelegate: UIResponder, UIApplicationDelegate {
我的AppDelegate
第一行代碼,我認為這是由於選項問題的解決? 有誰知道我是錯誤地使用函數還是有更好的方法呢?
嘗試這個
func forward() {
if currentPageIndex < totalPageIndex {
pageController?.setViewControllers([viewControllerAtIndex(index: currentPageIndex + 1)], direction: .forward, animated: true, completion: nil)
}
}
func backward() {
if currentPageIndex > 0 {
pageController?.setViewControllers([viewControllerAtIndex(index: currentPageIndex - 1)], direction: .reverse, animated: true, completion: nil)
}
}
如下更改代碼。 在編寫PageViewController()
這意味着您正在創建類型為PageViewController
新對象,並且嘗試在該對象實例上設置頁面,該頁面未添加到視圖中。
@objc func myOpinionAction(_ sender:UIButton!)
{
pageViewController.setViewControllers([suggestionPage()], direction: UIPageViewController.NavigationDirection.forward, animated: true)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.