I'm trying to add page to UIPageViewController
when user tap the table row, and jump to the new page in UIPageViewController
at the same time. I use the following code, but not run as wish.
The code in TableViewController class:
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")
}
}
The code in 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
}
When i tap the table row, presum 3 cities at beginning,
getCityList()
method is updated to 4 cities, even inpageRootController.presentationCountForPageViewController(pageController)
, but theUIPageControl dots
are still remain unchanged of 3, if i quit the app (not suspend in background), and restart, then theUIPageControl dots
update to 4. But I hope dots updated to 4 immediatelly when user taps the table row.Also I'm confused how to jump to the new added page view, the method in table row is incorrect, because it just jump to the
UIViewController
, not the addedUIPageViewController
.
Thank you very much in advance!