Advice needed on passing Count of records in rendered Mapview in Second View Controller to be displayed in First View Controller

Hi All. I am coding my first IOS App and have never coded before and am stuck on a crucial part.

The issue is that when the app is launched, a Dashboard is displayed to the User in the First View Controller.

  • There is a field on the Dashboard that needs be retrieved from the Second View Controller.
  • The field in the Second View Controller is the count of records from the mapViewDidFinishRenderingMap function.
  • Note this is a Storyboard app, with a Tab Bar Controller

The question is: What is the best way for the First View Controller to retrieve the count from the rendered map in the Second View Controller so that the count is displayed on the Dashboard to the User upon launching the App?

Thanks in advance for any suggestions!

Answered by OOPer in 677140022

What is the best way for the First View Controller to retrieve the count from the rendered map in the Second View Controller so that the count is displayed on the Dashboard to the User upon launching the App?

It is hard to say what would the best for your app, as it depends on the details of your view controllers.

So, this is just one example, but you can use sort of shared data container. Something like this:

class DataContainer {
    static let shared = DataContainer()
    
    var countOfRecords: Int = 0
    
    //...
}

class FirstViewController: UIViewController {
    let dataContainer = DataContainer.shared
    
    func someMethod() {
        let count = dataContainer.countOfRecords
        //...
    }
}


class SecondViewController: UIViewController {
    let dataContainer = DataContainer.shared
    
    func someAction() {
        //...
        dataContainer.countOfRecords = ...
    }
}

Are you really using SwiftUI? If you are working with Storyboard and view controllers, you may be using UIKit, not SwiftUI.

Sorry you are correct. I am using UIKit. My mistake

Thanks for clarification. You should better use the right tags, that would help you get the better responses sooner.

Accepted Answer

What is the best way for the First View Controller to retrieve the count from the rendered map in the Second View Controller so that the count is displayed on the Dashboard to the User upon launching the App?

It is hard to say what would the best for your app, as it depends on the details of your view controllers.

So, this is just one example, but you can use sort of shared data container. Something like this:

class DataContainer {
    static let shared = DataContainer()
    
    var countOfRecords: Int = 0
    
    //...
}

class FirstViewController: UIViewController {
    let dataContainer = DataContainer.shared
    
    func someMethod() {
        let count = dataContainer.countOfRecords
        //...
    }
}


class SecondViewController: UIViewController {
    let dataContainer = DataContainer.shared
    
    func someAction() {
        //...
        dataContainer.countOfRecords = ...
    }
}

OOPer, thanks I like your idea of using a data container.  However the count in the container is returning 0 on the First View Controller (Dashboard) and it is returning the correct number in the Second View Controller (Map View). I wonder if the issue is that the map in the Second View Controller is rendered after the First View Controller is loaded. So the First View Controller never gets the count.

The Data Container solution provided by OOPer is a good approach for this problem. I am using a data container to pass data to my main View Controller from a new Swift class that I am using to calculate the count of fires manually rather than counting the fires in the rendered map view.

Advice needed on passing Count of records in rendered Mapview in Second View Controller to be displayed in First View Controller
 
 
Q