gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:38:15 28 4
gpt4 key购买 nike

情况:我想从数据条目列表导航回我的 PageViewController。

before 和 previous 函数起作用

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() 中设置 UIPageViewControllerUIViewController(Location),我可以通过 UIPageViewController 到我的 UITableView 并且我能够导航回来并且一切正常。但是我想删除我的 UITableView 中的位置,当我删除我想返回的位置时(每个位置后面都是 CoreData 中的 dada)它崩溃了,因为数据在 CoreData 中消失了。

我尝试在 viewWillAppear(animated: Bool) 中设置 UIViewController(Location)。现在我可以删除条目,向后导航并拥有自己的设置 UIViewController(Location) BUT - 如果我滚动到上一个 View 的“副本”尽管 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) {

}

最佳答案

我找到了一个解决方案:

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() 中调用,我能够“清除现金 View ”,当我返回时,不再有前一个 View 的“副本”!

关于ios - 如何导航回已删除的 PageViewController 条目/更改 PageViewController 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37083297/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com