Search code examples
swiftuitableviewlabelsegue

How to make a segue append a a tableview in another Vc in swift?


Still learning and not very bright. In 2 VCs: the first one has some labels that perform a segue to a tableview in a second VC. Although the code says append, it works only once. There is an unwind from the second VC to the first, but when I push the button to append and "feed" the tableview, it does not append, it replace the first segue. I believe this is because the segue, since it will always shows a brand new VC and not append the tableview, this way I lose the first "insert". Or, there is an issue with the append code. Any help or sample how to append a tableview in another VC would be very helpful. I looked for a lot but it seems there is always a segue FROM tableview not TO a tableview. I thank in advance any thoughts.


Solution

  • You should create a new file ApplicationData.swift and write down following code:

    import Foundation
    
    class ApplicationData {
        static var shared = ApplicationData()
    
        var cities = ["Austin", "Barcelona", "Canberra", "Darwin", "Florida", "Moscow"]
    
        private init() {
            print("Shared instance succuessfully instantiated")
        }
    }
    

    in any of your view controller you should add variable:

    var applicationData = ApplicationData.shared
    

    No matter which controller changes cities property of the variable it will affect one and only instance shared within your application.