PageMenu如何动态(以编程方式)添加视图控制器?

3

我希望我的应用程序看起来像下面的图像。

我正在使用PageMenu来实现此结果。数据是从Web API获取的。我想根据Web API响应的天数动态创建视图控制器,以便每天都可以有一个视图控制器,以便它可以显示该天的时间表。如何实现这一点?

输入图像描述

2个回答

1

只需创建一个循环,多次添加相同的控制器。我创建了addPageMenu函数。在收到响应时调用它。

func addPageMenu(count: Int) {

 var controllerArray : [UIViewController] = []

  for i in count {

  var controller : UIViewController = UIViewController(nibName: "controllerNibName", bundle: nil)
  if i == 0 {
  controller.title = "SUNDAY"
  } else if i == 1 {
   controller.title = "MONDAY"
  }...

  controllerArray.append(controller)

 }

  let pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, 0.0, self.view.frame.width, self.view.frame.height), pageMenuOptions: parameters)

// Lastly add page menu as subview of base view controller view
// or use pageMenu controller in you view hierachy as desired
  self.view.addSubview(pageMenu!.view)
 }

1
阅读您提供的链接,它包含以下内容:
// Array to keep track of controllers in page menu
var controllerArray : [UIViewController] = []

// Create variables for all view controllers you want to put in the 
// page menu, initialize them, and add each to the controller array. 
// (Can be any UIViewController subclass)
// Make sure the title property of all view controllers is set
// Example:
var controller : UIViewController = UIViewController(nibName: "controllerNibName", bundle: nil)
controller.title = "SAMPLE TITLE"
controllerArray.append(controller)

所以你需要做的是:编写一个返回 [dailyModel] 的函数。 你的 dailyModel 应该长这样:
struct dailyModel {
let programStartingTime : String // 6:45
let item : [item] // [(Maghrib & Isha Prayer, 20, 0),(Ziarat-e-Ashura, 20, 1), ...otherItems...]
}

struct item{
let duration : Int? //minutes
let name : String // Maghrib and Isha prayers
let row : Int? //  0
}

然后循环遍历该 dailyModel,使用其参数实例化并填充视图控制器,然后像教程所做的那样将每个视图控制器append。这不是最好的代码,但我希望你能理解这个想法。

@AliMir 或许你的项目可以使用实际时间,比如下午6点15分(而不是持续时间和行数),然后使用类似这个答案的方法按照时间对item进行排序并列出它们。我之前没有处理过日期,但是我假设webAPI返回的是字符串格式,你需要将其转换为NSDate,然后按照时间进行排序,如果JSON数据是以字典格式呈现的,那么你就需要担心一下了,需要先将其转化为数组格式,然后循环打印每一个项目的时间和名称... - mfaani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接