0

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
}
  1. When i tap the table row, presum 3 cities at beginning, getCityList() method is updated to 4 cities, even in pageRootController.presentationCountForPageViewController(pageController), but the UIPageControl dots are still remain unchanged of 3, if i quit the app (not suspend in background), and restart, then the UIPageControl dots update to 4. But I hope dots updated to 4 immediatelly when user taps the table row.

  2. 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 added UIPageViewController.

Thank you very much in advance!

stephen
  • 519
  • 6
  • 22
  • I just posted a similar answer here: http://stackoverflow.com/a/32548471/4106164 Let me know if it helps. – Gordonium Sep 13 '15 at 10:06
  • No problem. But I think this question is more about UIPageViewController, I just don't know how to jump to the specific page view through table row tapping. – stephen Sep 14 '15 at 11:01
  • @Gordonium I have to say thank you, it's the simplest and easiest way solved my problem. I tried many, but finally find your answer is the best... I think I should try people's help before judge, Sorry for the last comments. – stephen Sep 26 '15 at 03:03

0 Answers0