2017-08-02 43 views
1

我已經尋找了許多不同的解決方案,但他們都不能通過按鈕成功轉換頁面。其中,我發現的方法之一可能會成功,但系統有一個未知的錯誤「不平衡的調用開始/結束外觀轉換」,所以我想知道是否有可行的方法來通過按鈕來轉換頁?是否可以通過使用Button來更改UIPageViewController中的當前頁面?

斯威夫特3:

public func changeVC(VC: UIViewController) { 
     self.setViewControllers([VC], direction: .forward, animated: true, completion: nil) 
} 

非常感謝你:)

+0

使用'protocol'模式。你的按鈕在哪裏?它在ViewController中滑動嗎? –

+0

檢查https://github.com/mattneub/pageViewControllerUsingInternalButtons –

+0

是的。該按鈕位於ViewController中,因爲我在PageViewController中有3張幻燈片。該按鈕將從PageViewController類調用changeVC方法。順便說一句,是不可能使用setViewControllers? –

回答

0

在ViewController中像這樣創建協議,其中的UIButton

import UIKit 

protocol welcomeVCdelegate { 
    func continueBtnPressed() 
} 

class welcomeVC: UIViewController { 

    var delegate : welcomeVCdelegate? 

然後在動作中調用該函數你創建於:

@IBAction func continueBtnAction(_ sender: UIButton) { 
     self.delegate?.continueBtnPressed() 
} 

現在給你的主Class協議:

class TutorialVC: UIViewController, welcomeVCdelegate { 

現在創建的是class實例:

var pushVC: welcomeVC! 

,並給予delegate你在哪裏instantiating

pushVC = self.storyboard?.instantiateViewController(withIdentifier: "welcomeVC") as! welcomeVC 
pushVC.delegate = self 

現在在這裏定義你的功能:

func continueBtnPressed() { 
    print("continueBtnPressed") 
     //Push to any view controller from here 
} 
0

使用下面的代碼:

//array with all pages 
private(set) lazy var orderedViewControllers: [UIViewController] = { 
    return [ 
       Utils.newViewController(name: "PhotoOfTheDayViewController"), 
       Utils.newViewController(name: "PhotosViewController"), 
       Utils.newViewController(name: "PhotosViewController"), 
       Utils.newViewController(name: "PhotosViewController"), 
       Utils.newViewController(name: "InfoViewController") 
    ] 
    }(); 
private var currentIndex : Int!; 

//jump to first page 
@objc private func home() { 
    self.jumptoPage(ind: 0); 
} 

private func jumptoPage(ind : Int) { 

    var index = ind; 
    let vc = self.orderedViewControllers.first; 
    let direction : UIPageViewControllerNavigationDirection!; 

    if currentIndex < index { 
     direction = .forward; 
     index += 1; 
    } else { 
     direction = .reverse; 
     index -= 1; 
    } 

    if (currentIndex < index) { 
     for i in 0 ..< index + 1 { 
      if (i == index) { 
       self.setViewControllers([vc!], direction: direction, animated: true, completion: nil); 
      } else { 
       self.setViewControllers([self.orderedViewControllers[i]], direction: direction, animated: false, completion: nil); 
      } 
     } 
    } else { 

     for i in stride(from: currentIndex, to: index - 1, by: -1) { 
      if i == index { 
       self.setViewControllers([vc!], direction: direction, animated: true, completion: nil); 
      } else { 
       self.setViewControllers([self.orderedViewControllers[i]], direction: direction, animated: false, completion: nil); 
      } 
     } 


    } 
    currentIndex = ind; 
} 
-1

我的解決方案,這如果需要在一個循環週期網頁:

 var pageIndex:Int? 
    var pageControl:UIPageControl = UIPageControl() 
    var pages = [UIViewController]() 
    var pager:UIPageControl? 

    var storyboardref: UIStoryboard { 
     get{ 
      return UIStoryboard(name: "Tutorial", bundle: nil) 
     } 
    } 

    var page1:UIViewController { 
     get{ 
      return storyboardref.instantiateViewController(withIdentifier: "page01") 
     } 
    } 
... 
    override func viewDidLoad() { 
     super.viewDidLoad() 
...  
     pages.append(page1) 
     pages.append(page2) 

     setViewControllers([pages[0]], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil) 
       pageControl.numberOfPages = pages.count 
     pageControl.currentPage = pageIndex! 
    } 

    override func setViewControllers(_ viewControllers: [UIViewController]?, direction: UIPageViewControllerNavigationDirection, animated: Bool, completion: ((Bool) -> Void)?) { 
     super.setViewControllers(viewControllers, direction: direction, animated: animated, completion: completion) 
     pageIndex = pages.index(of: viewControllers![0]) 
     pageControl.currentPage = pageIndex! 
    }  

    func nextPage() { 
     let nextIndex = abs((pageIndex! + 1) % pages.count) 
     self.setViewControllers([tutorial.pages[nextIndex]], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)  
    } 

    func previousPage() { 
     let prevIndex = abs((pageIndex! + pages.count - 1) % pages.count) 
     self.setViewControllers([pages[prevIndex]], direction: UIPageViewControllerNavigationDirection.reverse, animated: true, completion: nil) 

    } 
相關問題