Xlpagertabstrip: How to add Child Controller, When it is an uncertain number

Created on 16 Jun 2016  路  4Comments  路  Source: xmartlabs/XLPagerTabStrip

    override func viewControllersForPagerTabStrip(pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {

        var controllers = [UIViewController]()

        for _ in 0...kinds.count - 1 {
            let chid = SecondViewController()
            controllers.append(chid)
        }
        return controllers
    }

kinds.count is Child Controllers number but it request from internet ,it is an uncertain number

Most helpful comment

@khanxc sure, it is possible.

when you create the child view controller you need to assign the data it's going to display. Something like:

let child1 = MenuViewController()
child1.menuItems = [BurgerItem] // Populate the data beforehand
children.append(child1)  

let child2 = MenuViewController()
child2.menuItems = [ChickenItem]
children.append(child2)  

Same view controller, different data to display

All 4 comments

You can dynamically create any number of child view controllers as you do in the sample code. I create children in a loop as well and it works fine.

@volkanx @dowhilenet Like reusing the same viewconrtoller with different data? check attachment . Is this achievable with this framework?
img_1211
img_1210

@khanxc sure, it is possible.

when you create the child view controller you need to assign the data it's going to display. Something like:

let child1 = MenuViewController()
child1.menuItems = [BurgerItem] // Populate the data beforehand
children.append(child1)  

let child2 = MenuViewController()
child2.menuItems = [ChickenItem]
children.append(child2)  

Same view controller, different data to display

Well explained by @volkanx. I would just add that children is an array of UIViewControllers, therefore in the for loop you need to instantiate let child = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChildViewController") as! ChildViewController
That works fine for me.
Now the question i have is how can we deallocate some of the ViewControllers we don't need. For example, i would like to have 3 adjacent ViewControllers at the same time (so to keep smooth paging) and the rest should be deallocated so that the memory would be saved. My ViewControllers are quite heavy and thats the reason i would like them to be deallocated.
Any help would be appreciated

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rkittinger picture rkittinger  路  3Comments

luminkhant picture luminkhant  路  4Comments

bishalg picture bishalg  路  5Comments

unittesting0 picture unittesting0  路  3Comments

pramodshukla1011 picture pramodshukla1011  路  3Comments