简体   繁体   English

如何导航回已删除的PageViewController条目/更改PageViewController条目

[英]How navigate back to a deleted PageViewController entry / change the PageViewController entry

Situation: I want to navigate back from a list of data entries to my PageViewController. 情况:我想从数据条目列表导航回我的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. 问题:如果我从viewDidLoad()中的UIPageViewController设置UIViewController (Location),我可以通过UIPageViewController按钮到我的UITableView ,我可以导航回来,一切正常。 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. 但是我想在我的UITableView删除Locations,当我删除我想要导航回来的CoreData (在每个位置后面是CoreData中的CoreData )时它会崩溃,因为CoreData中的数据消失了。

I tried to set the UIViewController (Location) in the viewWillAppear(animated: Bool). 我试图在viewWillAppear(动画:Bool)中设置UIViewController (Location)。 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? 现在我能够删除条目,导航回来并拥有自己的设置UIViewController (位置) 我有 - 如果我滚动到上一个视图刚刚显示的“副本”虽然viewControllerBeforeViewController返回nil ...这怎么可能?

code snippet of the first viewDidLoad() version: 第一个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) {

}

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! 使用我在UIPageViewControllerviewWillAppear()调用的setViewControllerAndClearCache(vc: UIViewController) ,我可以“清除兑现的视图”,当我向后导航时,不再有前一个视图的“复制”了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM