Ali Mir Ali Mir - 1 year ago 119
iOS Question

PageMenu how to add view controllers dynamically (programmatically)?

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

Answer Source

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"

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download