如何导航回已删除的 PageViewController 条目/更改 PageViewController 条目 [英] How navigate back to a deleted PageViewController entry / change the PageViewController entry
问题描述
情况:我想从数据条目列表返回到我的 PageViewController.
Situation: I want to navigate back from a list of data entries to my PageViewController.
before 和 previous 函数起作用
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
}
问题:如果我在 viewDidLoad() 中从 UIPageViewController
设置 UIViewController
(Location),我可以通过 UIViewController
(Location)code>UIPageViewController 到我的 UITableView
并且我能够返回并且一切正常.但是我想删除 UITableView
中的位置,当我删除我想要导航回来的位置时(每个位置后面都是 CoreData
中的数据)它崩溃了,因为数据CoreData
中的数据不见了.
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.
我尝试在 viewWillAppear(animated: Bool) 中设置 UIViewController
(Location).现在我可以删除条目,向后导航并拥有自己的设置 UIViewController
(Location) BUT - 如果我滚动到上一个视图虽然 viewControllerBeforeViewController
返回 nil 只是显示...这怎么可能?
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?
第一个 viewDidLoad() 版本的代码片段:
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) {
}
推荐答案
我找到了解决方案:
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)
我在 UIPageViewController
的 viewWillAppear()
中调用它,我能够清除兑现视图",当我向后导航时,不再有上一个视图的副本"!
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!
这篇关于如何导航回已删除的 PageViewController 条目/更改 PageViewController 条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!