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

时间:2016-05-07 00:37:01

标签: ios swift uipageviewcontroller

情况:我想从数据条目列表导航回我的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()的UIViewController设置UIPageViewController(位置),我可以通过UIPageViewController的按钮转到我的UITableView我能够导航回来,一切正常。但我想删除UITableView中的位置,当我删除我想要导航回来的位置时(CoreData中每个位置后面都是dada),它会崩溃,因为CoreData中的数据走了。

我尝试在viewWillAppear(动画:Bool)中设置UIViewController(位置)。现在我可以删除条目,导航回来并拥有自己的集UIViewController(位置)但是我有 - 如果我滚动到上一个视图刚才显示的“副本”虽然viewControllerBeforeViewController返回nil ......这怎么可能?

第一个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) {

}

1 个答案:

答案 0 :(得分: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) UIPageViewController中呼叫viewWillAppear()我能够清除兑现的观看次数&#34;当我向后导航时,没有&#34;复制&#34;之前的观点了!