2016-05-07 138 views
1

情況:我想從數據條目列表導航回到PageViewController。如何導航回到已刪除的PageViewController條目/更改PageViewController條目

之前和以前職能的工作

func pageViewController(pageViewController: UIPageViewController, 
         viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? { 


    if ((viewController as! LocationViewController).mLocationIndex > 0){ 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let vc = storyboard.instantiateViewControllerWithIdentifier("LocationViewController") as! LocationViewController 
     vc.mLocationIndex = (viewController as! LocationViewController).mLocationIndex - 1 
     return vc 
    } 
    return nil 
} 

func pageViewController(pageViewController: UIPageViewController, 
         viewControllerAfterViewController viewController: UIViewController) -> UIViewController? { 

    let locCount = DataManager.sharedInstance.getLocationCount() 
    if ((viewController as! LocationViewController).mLocationIndex < locCount - 1){ 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let vc = storyboard.instantiateViewControllerWithIdentifier("LocationViewController") as! LocationViewController 
     vc.mLocationIndex = (viewController as! LocationViewController).mLocationIndex + 1 
     return vc 
    } 

    return nil 
} 

問題:如果我從viewDidLoad中的UIPageViewController(設置UIViewController(位置))我可以通過按鈕去從UIPageViewControllerUITableView和我我能夠導航回來,一切正常。但是我想刪除我的UITableView中的位置,當我刪除那個我想要導航的位置時(在每個位置後面都是CoreData中的dada),它會崩潰,因爲CoreData中的數據已經消失。

我試着在viewWillAppear(animated:Bool)中設置UIViewController(位置)。現在我可以刪除條目,導航回來,並擁有自己的設置UIViewController(位置)但是我有 - 如果我滾動到上一個視圖「剛剛顯示的副本」,雖然viewControllerBeforeViewController返回零...如何這可能嗎?第一viewDidLoad中的

代碼段()版本:

class MainPageViewController: UIPageViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    dataSource = self 

    let dM = DataManager.sharedInstance 
    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let vc = storyboard.instantiateViewControllerWithIdentifier("LocationViewController") as! LocationViewController 
    dM.mActualLocationIndex = 0 

    // check if the index is too big (deleted data) 
    if (dM.mActualLocationIndex < dM.getLocationCount()){ 
     vc.mLocationIndex = dM.mActualLocationIndex 
    } else { 
     vc.mLocationIndex = 0 
     dM.mActualLocationIndex = 0 
    } 
    vc.mLocationIndex = 0 
    setViewControllers([vc], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil) 

} 

override func viewWillAppear(animated: Bool) { 

} 

回答

0

我找到了一個解決方案:

extension UIPageViewController { 
    func setViewControllerAndClearCache(vc: UIViewController){ 
     setViewControllers([vc], direction: .Forward, animated: true, completion: { _ in 
      dispatch_async(dispatch_get_main_queue(), { 
       self.setViewControllers([vc], direction: .Forward, animated: false, completion: nil)     
      }) 
     }) 
    } 
} 

隨着setViewControllerAndClearCache(vc: UIViewController)我在UIPageViewControllerviewWillAppear(),我能夠「清除通話兌現的觀點「,當我回到那裏時,沒有任何更多的前一個視圖的」複製「!