Thanks I did not create a separate class for the tab bar view controller but think I know how to do it. But how would I indicate which tab bar to add the image function? Please forgive my questions as I have never coded before nor created an app. Could you please provide more guidance?
Post
Replies
Boosts
Views
Activity
OOPer you are awesome! It works now. I am very, very grateful you have taken the time to teach me about the error handling and the completion handler. The code for my App is so much better now and returns the fire count to my dashboard, and will give insights to any errors. Cheers to you!
Thanks! I guess this new forum needs lots of "enhancements".
sorry I don't know how to format the comments yet :(
OOPer, thanks I like your idea of using a data container to pass the count of records rendered on the map in the Second View Controller to the First View Controller (Dashboard).
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.
Here is the code for the First View Controller (Dashboard) followed by the code for the Second View Controller (Map View Controller). Any thoughts would be greatly appreciated!
First View Controller (Dashboard):
`// ViewController.swift
import Foundation
import UIKit
import Alamofire
import SwiftyJSON
import CoreLocation
import MapKit
class DataContainer {
static let shared = DataContainer()
var fireCount: Int = 0
}
class ViewController: UIViewController, CLLocationManagerDelegate {
let dataContainer = DataContainer.shared
@IBOutlet weak var FireAndWindView: UIView!
@IBOutlet weak var BackgroundView: UIView!
@IBOutlet weak var LocationLabel: UILabel!
@IBOutlet weak var Label: UIView!
@IBOutlet weak var WindLabel: UILabel!
@IBOutlet weak var WindDirectionLabel: UILabel!
@IBOutlet weak var TemperatureImage: UIImageView!
@IBOutlet weak var TemperatureLabel: UILabel!
@IBOutlet weak var FireStatusLabel: UILabel!
@IBOutlet weak var DateTimeLabel: UILabel!
@IBOutlet weak var AQILabel: UILabel!
@IBOutlet weak var AQISource: UILabel!
@IBOutlet weak var AQIManImage: UIImageView!
@IBOutlet weak var AirQualityDescriptionLabel: UILabel!
let WorldAQIApikey = ""
let AirNowApikey = ""
let WeatherMapApikey = ""
var lat = Double()
var lon = Double()
var locationManager = CLLocationManager()
var geocoder = CLGeocoder()
var fireCounts = Int()
override func viewDidLoad() {
super.viewDidLoad()
//Initialize data in all three Tab Bar Controller views upon launch of App by User
tabBarController?.viewControllers?.forEach { let _ = $0.view }
// Retrieve Fire Count from the FireMapViewController
func addFireCounttoDashboard() {
let fireCounts = dataContainer.fireCount
print (fireCounts)}
//Code to override default UI Background set Background UI Layer to Blue Gradient colors
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor(red: 95.0/255.0, green: 165.0/255.0, blue: 1.0, alpha: 1.0).cgColor, UIColor(red: 72.0/255.0, green: 114.0/255.0, blue: 184.0/255.0, alpha: 1.0).cgColor]
gradientLayer.frame = view.frame
view.layer.addSublayer(gradientLayer)
view.layer.insertSublayer(gradientLayer, at: 0)
//Function to handle User Location Services
if (CLLocationManager.locationServicesEnabled()){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 50
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
}
}
//User Location Authorization
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .denied: // Setting option: Never
print("LocationManager didChangeAuthorization denied")
case .notDetermined: // Setting option: Ask Next Time
print("LocationManager didChangeAuthorization notDetermined")
case .authorizedWhenInUse: // Setting option: While Using the App
print("LocationManager didChangeAuthorization authorizedWhenInUse")
// Request a one-time location information
locationManager.requestLocation()
case .authorizedAlways: // Setting option: Always
print("LocationManager didChangeAuthorization authorizedAlways")
// Request a one-time location information
// locationManager.requestLocation()
case .restricted: // Restricted by parental control
print("LocationManager didChangeAuthorization restricted")
default:
print("LocationManager didChangeAuthorization")
}
// func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
let accuracyAuthorization = manager.accuracyAuthorization
switch accuracyAuthorization {
case .fullAccuracy:
print("LocationManager: fullAccuracy")
break
case .reducedAccuracy:
print("LocationManager: User selected 'Precise = Off' reducedAccuracy")
// Create Temporary Request for Precise Location
manager.requestTemporaryFullAccuracyAuthorization(withPurposeKey: "AirQualityAccuracy")
print("LocationManager: User selected 'Precise = Off', then selected one time only Full Accuracy prompt reducedAccuracy")
break
default:
break
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("LocationManager didFailWithError \(error.localizedDescription)")
if let error = error as? CLError, error.code == .denied {
// Location updates are not authorized.
// To prevent forever looping of `didFailWithError` callback
locationManager.stopMonitoringSignificantLocationChanges()
return
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Add Fire Count and Status to Dashboard Screen
DispatchQueue.main.async {
self.FireStatusLabel.text = (String(self.fireCounts) + " fires within 100 miles of your location.")}
}
}`