情況:我想從數據條目列表導航回到PageViewController。如何導航回到已刪除的PageViewController條目/更改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中的UIPageViewController
(設置UIViewController
(位置))我可以通過按鈕去從UIPageViewController
我UITableView
和我我能夠導航回來,一切正常。但是我想刪除我的UITableView
中的位置,當我刪除那個我想要導航的位置時(在每個位置後面都是CoreData
中的dada),它會崩潰,因爲CoreData
中的數據已經消失。
我試着在viewWillAppear(animated:Bool)中設置UIViewController
(位置)。現在我可以刪除條目,導航回來,並擁有自己的設置UIViewController
(位置)但是我有 - 如果我滾動到上一個視圖「剛剛顯示的副本」,雖然viewControllerBeforeViewController
返回零...如何這可能嗎?第一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) {
}