我正在尝试在用户点按表格行时将页面添加到UIPageViewController
,然后同时跳转到UIPageViewController
中的新页面。我使用以下代码,但没有按照愿望运行。
TableViewController类中的代码:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let pageRootController=self.storyboard!.instantiateViewControllerWithIdentifier("PageRootViewController") as! PageRootViewController
switch indexPath.section {
case O:
addNewCity(hotCityList[indexPath.row])
var savedCityArray = getCityList()
pageRootController.pageControlAppearance()
pageRootController.createPageViewController()
pageRootController.presentationCountForPageViewController(pageController)
pageRootController.presentationIndexForPageViewController(pageController)
var newViewController = pageRootController.getItemController(savedCityArray.count-1)
presentViewController(newViewController!, animated: true, completion: nil)
default:
addNewCity("N/A CITY")
}
}
PageRootViewController中的代码:
func getItemController(itemIndex: Int) -> PageContentViewController? {
var userCityArray = getCityList()
if itemIndex < userCityArray.count{
let PageContentController=self.storyboard!.instantiateViewControllerWithIdentifier("PageContentController") as! PageContentViewController
//code to configure PageContentController
return PageContentController
}
return nil
}
private var pageViewControlelr: UIPageViewController?
func createPageViewController(){
let pageController=self.storyboard!.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
pageController.dataSource=self
var userCityArray = getCityList()
if userCityArray.count>0 {
let firstController=getItemController(0)!
let startingViewControllers: NSArray = [firstController]
pageController.setViewControllers(startingViewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}
pageViewControlelr=pageController
addChildViewController(pageViewControlelr!)
self.view.addSubview(pageViewControlelr!.view)
pageViewControlelr!.didMoveToParentViewController(self)
}
func pageControlAppearance() {
//configure pageControl Appearance
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
//delegate method
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
//delegate method
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
var userCityArray = getCityList()
return userCityArray.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
func getCityList () -> NSMutableArray {
let paths=NSSearchPathForDirectoriesInDomains(
NSSearchPathDirectory.DocumentDirectory,
NSSearchPathDomainMask.UserDomainMask,
true)
let docDir: String=paths[0] as! String
let filePath: String = docDir.stringByAppendingPathComponent("savedCity.txt")
if !NSFileManager.defaultManager().fileExistsAtPath(filePath){
NSFileManager.defaultManager().createFileAtPath(filePath, contents: nil, attributes: nil)
var savedCityArray:NSMutableArray=[""]
savedCityArray[0]=""
savedCityArray.writeToFile(filePath,atomically:true)
}
var savedCityArray: NSMutableArray = NSMutableArray(contentsOfFile: filePath)!
return savedCityArray
}
当我点按表格行时,预设3个城市,getCityList()
方法更新为4个城市,即使在pageRootController.presentationCountForPageViewController(pageController)
,但UIPageControl dots
仍保持不变3,如果我退出应用程序(不在后台暂停),然后重新启动,则UIPageControl dots
更新为4.但我希望当用户点击表格行时,点会立即更新为4。
另外我很困惑如何跳转到新添加的页面视图,表格行中的方法不正确,因为它只是跳转到UIViewController
,而不是添加的UIPageViewController
。
非常感谢您提前!