3

I want my app to look like the image below.

I am using PageMenu to acomplish this result. The data is fetcehd from a web API. I want to dynamically create view controllers based on the number of days that the web API responds with so that each day can have a view controller so that it can display schedule for that day. How can I accomplish this?

enter image description here

Ali Mir
  • 565
  • 7
  • 20

2 Answers2

1

Simply create a loop add same controller multiple times. i created addPageMenu function. call it when you get the response.

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)
 }
Sahil
  • 9,096
  • 3
  • 25
  • 29
1

reading from the link you provided yourself it has:

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

So what you have to do is : have a function that returns a [dailyModel]. Your dailyModel would look something like this:

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
}

Then loop through that dailyModel and using its parameters you instantiate & populate viewcontroller and then append each of them as the tutorial is doing.

This isn't the best code but I hope you get the idea.

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • @AliMir or perhaps your items could have **actual times** ie *6:15 PM* (instead of duration and row) and then sort (using something like [this answer](http://stackoverflow.com/a/34800848/5175709)) your `item`s based on time and list them. I haven't worked with dates before, but I'm assuming the webAPI returns in a string format which you would later have to convert into `NSDate`s and then sort/them if it's in a dictionary format, if your JSON is in an Array format then you have nothing to worry about and you can just loop through and print an item's time and name... – mfaani Nov 16 '16 at 15:27