Search code examples
iosswiftdelegatesprotocols

How to avoid a trail of delegate calls in a navigation stack?


I've search result on View A. I've a filter options on search result. When suer clicks on filter view A it takes him to view B which lists all the filter options. Clicking on filters on B will take user to View C which shows selection for each filter. When user selects filters on view C I want to pass it back to A so that when user goes back to view A, after selecting filters, result data on A is updated after applying selected filter.

How to do that in any other simplest way possible?


Solution

  • There are many ways. I will share the easiest. Create a modal struct, update its value from view controller C and then use it in A.

     struct StudentData { 
        static var name = ""
        static var address = ""
     }
    

    In View Controller C, update the values

    StudentData.name = "your name" 
    StudentData.address = "your address"
    

    In View Controller A,

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        nameField.text = StudentData.name
        addressField.text = StudentData.address
    }