问题
Situation: I want to navigate back from a list of data entries to my PageViewController.
the before and previous functions work
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
}
Problem: If I set the UIViewController
(Location) from the UIPageViewController
in the viewDidLoad() I can go via button from the UIPageViewController
to my UITableView
and I am able to navigate back and everything works fine. But I want to delete Locations in my UITableView
and when i delete the one where i want to navigate back (behind every Location are dada in the CoreData
) it crashes because the data in CoreData
are gone.
I tried to set the UIViewController
(Location) in the viewWillAppear(animated: Bool). Now I am able to delete entries, navigate back and have the own set UIViewController
(Location) BUT i have - if i scroll to the previous view a "copy" of the just displayed although the viewControllerBeforeViewController
return nil... how is this possible?
code snippet of the first viewDidLoad() version:
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) {
}
回答1:
I found a solution:
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)
})
})
}
}
With setViewControllerAndClearCache(vc: UIViewController)
which i call in the UIPageViewController
's viewWillAppear()
i am able to "clear the cashed views" and when i navigate back there is no "copy" of the previous view any more!
来源:https://stackoverflow.com/questions/37083297/how-navigate-back-to-a-deleted-pageviewcontroller-entry-change-the-pageviewcon