2017-02-28 63 views
0

我有一個UIPageViewController,處理一個數組viewControllers 當我在他們之間滑動一切都很好,但我有幾個方法(成功登錄後,菜單按鈕點擊後)觸發'跳轉到UIPageViewControllers陣列中的viewController。 每當我跳轉到一個頁面,過渡不完整的,我有一個在兩個viewControllersUIPageViewController沒有完成按鈕點擊轉換

之間「卡住」爲了簡單的畫面,我只會添加成功登錄的代碼,因爲我設置的一切同爲按鈕點擊,同樣的事情發生。

這是我UIPageViewController設置:

import UIKit 

class PageViewController: UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate , ClubViewControllerDelegate , CustomSvDelegate{ 

//clubvcdelegate protocol method 

func fbLoggedInWithSuccess(){ 
self.displayPageForIndex(index: 4, animated: false) 
} 

//menubuttondelegate protocol method 
func menuButtonPressed0() { 

    self.displayPageForIndex(index: 0, animated: false) 
    self.currentPageIndex = 0 
    self.closeMenu() 
} 

//page swipe control 

lazy var vcArray : [UIViewController] = { 
    return [self.vcInstance (name : "clubVC"), 
      self.vcInstance (name : "menuVC"), 
      self.vcInstance (name : "contactVC"), 
      self.vcInstance (name : "aboutVC"), 
      self.vcInstance (name : "membersVC")] 
    }() 

private func vcInstance(name : String) -> UIViewController{ 
    let storyBoard = UIStoryboard(name: "Main", bundle: nil) 
    return storyBoard.instantiateViewController(withIdentifier: name) 
} 


override func viewDidLoad() { 
    super.viewDidLoad() 
    self.dataSource = self 
    self.delegate = self 
    if let clubVC = vcArray.first { 
     let club = clubVC as! ClubViewController 
     club.delegate = self 
     setViewControllers([clubVC], direction: .forward, animated: true, completion: nil) 
    } 
    self.generateStackView() 
} 


override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 
    for view in self.view.subviews { 
     if view is UIScrollView {view.frame = UIScreen.main.bounds} else if view is UIPageControl {view.backgroundColor = UIColor.clear} 
    } 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{ 
    guard let viewControllerIndex = vcArray.index(of: viewController) else {return nil} 

    let previousIndex = viewControllerIndex - 1 

    guard previousIndex >= 0 else {return vcArray.last} 

    guard vcArray.count > previousIndex else {return nil} 

    return vcArray[previousIndex] 

} 


public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{ 
    guard let viewControllerIndex = vcArray.index(of: viewController) else {return nil} 

    let nextIndex = viewControllerIndex + 1 

    guard nextIndex < vcArray.count else {return vcArray.first} 

    guard vcArray.count > nextIndex else {return nil} 

    return vcArray[nextIndex] 

} 

public func presentationCount(for pageViewController: UIPageViewController) -> Int{ 
    return vcArray.count 
} 


public func presentationIndex(for pageViewController: UIPageViewController) -> Int{ 
    guard let firstViewController = viewControllers?.first , 
    let firstViewControllerIndex = vcArray.index(of: firstViewController) else { 
     return 0 
    } 
    return firstViewControllerIndex 
} 

,這是我建立的東西在那裏fblogin是成功的:

protocol ClubViewControllerDelegate:class { 
func fbLoggedInWithSuccess() 
} 

class ClubViewController: UIViewController , FBSDKLoginButtonDelegate { 
var delegate:ClubViewControllerDelegate? 

func showFbDetails(){ 

    let accesToken = FBSDKAccessToken.current() 

    guard let accesTokenString = accesToken?.tokenString else {return} 

    let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accesTokenString) 

    FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in 
     if error != nil { 
      print ("something went wrong" , error!) 
      return 
     } 
     print ("succes") 
     self.delegate?.fbLoggedInWithSuccess() 
    }) 

} 

圖片: enter image description here enter image description here

EDIT2 這只是發生在動畫真的,假的時,在完成正常

EDIT3 這是方法我現在用它來設置視圖控制器:

func displayPageForIndex(index : Int , animated : Bool = true){ 
    assert(index >= 0 && index < self.vcArray.count, "Error: Attempting to display a page for an out of bounds index") 
    if self.currentPageIndex == index {return} 
    if index < self.currentPageIndex { 
     self.setViewControllers([self.vcArray[index]], direction: .reverse, animated: false, completion: nil) 
    } else if index > self.currentPageIndex { 
     self.setViewControllers([self.vcArray[index]], direction: .forward, animated: false, completion: nil) 
    } 
} 

我把它叫做fbLoggenInWithSuccessmenuButtonPressed內。 此外,具有可變CURRENTINDEX保存的頁面

+0

解釋你的代碼的目的,請。你爲什麼要調用'setViewControllers'兩次?什麼是弱的自我'pvcw'?什麼是「異步」? – matt

+0

我不再使用它。但是我想要做的是在登錄成功/點擊按鈕後跳轉到新的viewController。據我所知setviewcontrollers是我的唯一方法來更改當前viewController @matt –

+0

正確,但您展示的代碼是非常奇怪的,我想知道爲什麼你這樣做,是否它可能會導致問題。我只能繼續說你使用的代碼,而你說這就是你正在使用的代碼。當你這樣說的時候你是在說謊嗎?如果是這樣,爲什麼?如果這不是真正的代碼,請顯示您的_real_代碼。不可能在不知道真實代碼的情況下重現問題或猜測其原因。 – matt

回答

0

當前指數馬特建議刪除viewDidLayoutSubviews解決了這個問題

+1

很高興你找到它!通常的做法是放置一個屬性標誌,以便在第一次佈置子視圖時再執行定製代碼。 - 我對視圖控制器數組的批評仍然存在。這是錯誤的行爲。 – matt

+0

我會閱讀有關它並相應地更改,謝謝! –