2017-08-02 10 views
1

私はさまざまなソリューションを探しましたが、どれもボタンを使ってページを正常に変換することはできません。その中で、私が見つけた方法の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

はい。 PageViewControllerに3つのスライドがあるので、ボタンはViewControllerにあります。ボタンはPageViewControllerクラスからchangeVCメソッドを呼び出します。 Btw、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! 

そして、あなたがinstantiatingあるdelegateを与える:今すぐ

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) 

    } 
関連する問題