estoril
estoril

Reputation: 115

iOS cycling between view controllers in a navigation stack

I have been tasked with creating a flow in an iOS application where a user can add multiple steps, where the number of steps are undefined, each step acting as a ViewController within a navigation stack and the user can add multiple steps (VCs) while moving backwards in the navigation stack to edit, while moving back forwards to an existing step and not losing any inputted data.

Example: User creates Step 1, User creates step 2, user creates Step 3, user moves back to Step 2, User moves back to Step 1, Edits Info, moves forward to Step 2, moves forward to Step 3, etc.

So far I'm thinking of creating a sort of counter to keep track of what Step the user is on in addition to an array of Classes that hold the data that builds each VC but I'm having a little trouble with VC initializers and navigationController pushing and popping.

Any help would be appreciated, maybe someone has something up their sleeve.

Upvotes: 0

Views: 60

Answers (2)

Satish
Satish

Reputation: 2043

You should create a class to hold steps objects

Something like this

class DataClass {
    static let shared = DataClass()

    var arrayObjects: [Any]

    private init() {
        arrayObjects = []
    }

    func addObject(object: Any) -> [Any] {
        arrayObjects.append(object)
        return arrayObjects
    }

    func clearObjects() {
        arrayObjects.removeAll()
    }

    func object(at step: Int) -> Any? {
        guard arrayObjects.count > step else {
            return nil
        }

        return DataClass.shared.arrayObjects[step]
    }
}

And use data something like this in each step.

DataClass.shared.object(at: step)

Upvotes: 1

Hitesh Surani
Hitesh Surani

Reputation: 13577

You can achieved above requirement by storing your ViewController locally into array.

Step:1 Create global Array of UIViewController as below.

var aryAllViewController = [UIViewController]()

Step:2 Append value into Array as below.

aryAllViewController.append(VC)

Step:3 Get old ViewController reference from Array and Push it into Navigation Stack again.

If you follow above step properly then old data will display automatically.

Upvotes: 1

Related Questions