XLPagerTabStrip

时间:2017-10-16 09:15:12

标签: ios json ios11 swift4 xlpagertabstrip

我正在使用XLPagerTabStrip在Swift 4中创建基于类别的阅读应用程序。我学习了静态数量的ViewControllers,可以使用以下函数轻松创建。

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { } 

但是,我的案例中的类别数量取决于服务器响应,可能会根据需要进行更改。我试图通过根据我从json响应中解析的类别名称创建视图控制器来创建动态数量的选项卡。这是我打击和试用的方法。

    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
}

是的,它失败了。在这种情况下,如何实现动态数量的标签?如果不是,我也对任何其他选择开放。我完成了json响应的事情,但坚持这一步。此SO answerGithub Issue也无济于事。

3 个答案:

答案 0 :(得分:1)

我有完全相同的情况。如果您异步地从服务器获取结果,则会发生崩溃,因为xlpagertabstrip至少需要一个子视图控制器。 我找到的解决方案是在应用程序启动时返回一个虚拟视图控制器,并在从服务器获取数据到reloadPagerTabStripView之后返回。

在父视图控制器中,您应该创建一个从服务器获取的对象的空数组,例如:

var menuItems = [MenuObject]()

接下来,viewControllers(用于pagerTabStripController ...)方法应如下所示:

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
    var children = [UIViewController]()
    if menuItems.count == 0 {
        let child = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DummyVC") as! DummyVC
        children.append(child)
        return children
    } else {
        let menuItemsUrls = menuItems.map{$0.url}
        for menuItem in menuItems {
            let child = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainVC") as! TelegrafMainVC
            child.name = menuItem.name.uppercased
            child.url = menuItem.url
            children.append(child)
        }

        return children
    }
}

从服务器获取数据后,无论是使用URLSession,还是Alamofire,还是其他任何东西,在函数或闭包中都应该输入类似的内容:

self.menuItems = menuItems

 DispatchQueue.main.async {
       self.reloadPagerTabStripView()
 }

告诉我是否需要澄清一些事情,但我相信这足以帮助你。 干杯

答案 1 :(得分:0)

试试这个

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
}

答案 2 :(得分:0)

步骤1:在初始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)
 }
}

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

第3步:您的标签视图控制器:

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