2017-10-16 4 views
0

Ich benutze XLPagerTabStrip, um eine kategoriebasierte Lese-App in Swift 4 zu erstellen. Ich lernte statische Nummer von ViewControllern kann leicht mit der folgenden Funktion erstellt werden.Dynamische Anzahl der ChildViewControllers für XLPagerTabStrip

Allerdings hängt die Anzahl der Kategorien in meinem Fall von der Serverantwort ab, die sich je nach Bedarf ändern kann. Ich habe versucht, eine dynamische Anzahl von Registerkarten zu erstellen, indem ich View-Controller basierend auf dem Namen der Kategorien, die ich aus der json-Antwort analysiert habe, erstellt habe. Dies ist die Methode, die ich getroffen und getestet habe.

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 
} 

Ja, es ist fehlgeschlagen. Wie kann ich in diesem Fall eine dynamische Anzahl von Tabs erreichen? Wenn nicht, bin ich auch offen für jede andere Alternative. Ich bin mit JSON Antwort Sache fertig, aber steckte in diesem Schritt. Diese SO answer und Github Issue half auch nicht.

Antwort

0

Versuchen Sie, diesen

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

Erprobt und getestet auch, gibt fatalError ("ViewControllers (für :) sollte mindestens einen Kind View-Controller zur Verfügung stellen") Fehler – amagain

+0

Dies ist aufgrund "ChildrenVC" ist leer, stellen Sie bitte sicher, "postCategories" ist nicht leer – YinKiet

0

Schritt 1: Tun Sie dies in Ihrem ersten Viewcontroller,

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) 
} 
} 

Schritt 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() 
    } 
} 

Schritt 3: Ihr Tab-View-Controller:

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) 
    } 
} 
Verwandte Themen