2017-10-16 4 views
0

J'utilise XLPagerTabStrip pour créer une application de lecture de catégorie dans Swift 4. J'ai appris le nombre statique de ViewControllers peut être facilement créé en utilisant la fonction suivante. Cependant, le nombre de catégories dans mon cas dépend de la réponse du serveur, qui peut changer selon le besoin. J'ai essayé de créer un nombre dynamique d'onglets en créant des contrôleurs de vue basés sur le nom des catégories que j'ai analysées à partir de la réponse de JSON. C'est la méthode que j'ai fait un coup et un procès.Nombre dynamique de ChildViewControllers pour XLPagerTabStrip

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { 
    var childrenVC = [UIViewController]() 
    for eachCategory in postCategories { 
      print(eachCategory) 
     let newVC = self.storyboard?.instantiateViewController(withIdentifier: "FirstTVC") as? FirstTVC 
     newVC?.childName = eachCategory.name 
     childrenVC.append(newVC!) 
     self.reloadPagerTabStripView() 
     self.reloadInputViews() 
} 
    return childrenVC 
} 

Oui, il a échoué. Comment puis-je obtenir un nombre dynamique d'onglets dans ce cas? Sinon, je suis également ouvert à toute autre alternative. J'ai fini avec la réponse de JSON mais coincé dans cette étape. Ce SO answer et Github Issue n'a pas aidé aussi bien.

Répondre

0

Essayez cette

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { 
    var childrenVC: [UIViewController] = [] 

    for eachCategory in postCategories { 
     let newVC = UIStoryboard(name: "Main (YOUR-STORYBOARD-NAME)", bundle: nil).instantiateViewController(withIdentifier: "FirstTVC") as? FirstTVC 
     newVC?.childName = eachCategory.name 
     childrenVC.append(newVC!) 
    } 
    return childrenVC 
} 
+0

Essayé et testé cela aussi, donne fatalError ("viewControllers (pour :) devrait fournir au moins un contrôleur enfant") erreur – amagain

+0

cela est dû à "childrenVC" est vide, s'il vous plaît assurez-vous que "postCategories" n'est pas vide – YinKiet

0

Étape 1: Faites ceci dans votre viewcontroller initiale,

class initialViewController: UIViewController { 
    override func viewWillAppear(_ animated: Bool) { 
      self.loadMainView(viewControllerArray: self.setMainViewTabParameters()) 
    } 

func setMainViewTabParameters() -> NSMutableArray { 
     let viewControllerArray = NSMutableArray() 
     var tabArray = ["Tab1", "Tab2", "Tab3", "Tab4", "Tab5", "Tab6", "Tab7"] 

     var tabIndex = NSInteger() 
     for item in tabArray { 
      let tabString = tabArray[tabIndex] 
      let tabview = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "YourTabControllerIdentifier") as! YourTabController 
      tabview.tabHeadingTitle = tabString as NSString 
      viewControllerArray.add(tabview) 
      tabIndex = tabIndex + 1 
     } 
     return viewControllerArray 
} 

func loadMainView(viewControllerArray : NSMutableArray) -> Void { 
     let mainView = self.storyboard?.instantiateViewController(withIdentifier: "YourMainControllerIdentifier") as! YourMainController 
     mainView.viewControllerList = viewControllerArray 
     self.navigationController?.pushViewController(mainView, animated: false) 
} 
} 

Étape 2:

class YourMainController: ButtonBarPagerTabStripViewController { 

    var viewControllerList = NSArray() 
    var isReload = false 

    //MARK: - PagerTabStripDataSource 
    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { 
      guard isReload else { 
       return viewControllerList as! [UIViewController] 
      } 

      var childViewControllers = viewControllerList as! [UIViewController] 

      for (index, _) in childViewControllers.enumerated(){ 
       let nElements = childViewControllers.count - index 
       let n = (Int(arc4random()) % nElements) + index 
       if n != index{ 
        swap(&childViewControllers[index], &childViewControllers[n]) 
       } 
      } 
      let nItems = 1 + (arc4random() % 8) 
      return Array(childViewControllers.prefix(Int(nItems))) 
    } 

    override func reloadPagerTabStripView() { 
      isReload = true 
      if arc4random() % 2 == 0 { 
       pagerBehaviour = .progressive(skipIntermediateViewControllers: arc4random() % 2 == 0, elasticIndicatorLimit: arc4random() % 2 == 0) 
      } else { 
       pagerBehaviour = .common(skipIntermediateViewControllers: arc4random() % 2 == 0) 
      } 
      super.reloadPagerTabStripView() 
    } 
} 

Étape 3: Votre onglet contrôleur de vue:

class YourTabController: UIViewController, IndicatorInfoProvider { 
    var tabHeadingTitle = NSString() 

    // MARK: - Top Tab Bar Method - IndicatorInfoProvider 
    func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo { 
      return IndicatorInfo(title: tabHeadingTitle as String) 
    } 
}