对于这一个道歉,我确信我在这里错过了一个快速的基础。 我有一个导航应用程序,我正在迅速工作。 之前我把它作为单个视图,并将其更改为标签栏控制器应用程序。
在后台,位置管理器完成其工作,更新位置何时起作用,它会在标签上显示lat和long。
当应用程序是单个视图应用程序时,这不是问题,但是现在它是一个标签栏应用程序,当我在位置管理器功能触发时更改为其他选项卡时,它会使用以下代码崩溃应用程序(我怀疑这意味着没有什么可以自己的)EXC_BREAKPOINT (code=1, subcode=0x10039bb54
我强烈怀疑我在尝试更新当时未处于活动状态的视图控制器上的标签方面做了一些根本性的错误。
distanceLabel.text = "Currently \(distanceText) from destination"
lbl_location.text = String(mylat) + ", " + String(mylong)
我已经在下面的所有代码中复制了一些敏感位,以便您可以看到全局。
向更好的编码实践提供建设性的反馈。
由于
布鲁斯
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
//in the original view controller which has now been incorporated into tabs
@IBOutlet weak var distanceLabel: UILabel!
@IBOutlet weak var lbl_location: UILabel!
//on a new view controller on another tab
@IBOutlet weak var employeeNameSetup: UITextField!
@IBOutlet weak var employeeNumberSetup: UITextField!
@IBOutlet weak var contactNumberSetup: UITextField!
@IBOutlet weak var managerNameSetup: UITextField!
@IBOutlet weak var managerEmailSetup: UITextField!
@IBOutlet weak var adminPasswordSetup: UITextField!
@IBAction func setAppDefaults(_ sender: Any) {
if (employeeNameSetup.text?.isEmpty)! || (employeeNumberSetup.text?.isEmpty)! || (contactNumberSetup.text?.isEmpty)! || (managerNameSetup.text?.isEmpty)! || (managerEmailSetup.text?.isEmpty)!{
let noGoAlert = UIAlertController(title: "Insufficient Fields Filled out", message: "Only the Password field is optional, please fill out all other fields before pressing button", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Close Alert", style: .default, handler: nil)
noGoAlert.addAction(defaultAction)
present(noGoAlert, animated: true, completion: nil)
}
else{
let defaults = UserDefaults.standard
defaults.set(employeeNameSetup.text, forKey: "employeeName")
defaults.set(employeeNumberSetup.text, forKey: "employeeNumber")
defaults.set(contactNumberSetup.text, forKey: "contactNumber")
defaults.set(managerNameSetup.text, forKey: "managerName")
defaults.set(managerEmailSetup.text, forKey: "managerEmail")
}
}
@IBAction func submit_button(_ sender: Any) {
uploadData()
}
let location_process = CLLocationManager()
var mylat :Double = 0
var mylong :Double = 0
var destination = CLLocation(latitude: #####, longitude: #####)
var distanceToDest :Int = 0
var dburl = NSURL(string: "#########")
let contact_name = "#####"
let contact_number = "#####"
let unique_id = "###"
let employee_number = "#####"
var distanceText = ""
var runOnce = false
var isAdmin = false
var adminPassword = "#######"
func sendEmail(messageText: String){
let url = URL (string: "########")
let session = URLSession.shared
var request = URLRequest(url: url!)
request.httpMethod = "POST"
let paramtosend = #########
request.httpBody = paramtosend.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request)
task.resume()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let mylocation = locations[0]
mylat = mylocation.coordinate.latitude
mylong = mylocation.coordinate.longitude
distanceToDest = Int(mylocation.distance(from: destination))
if distanceToDest > 1000 {
distanceText = String(Int(distanceToDest/1000))
distanceText = distanceText + " km"
}
else if distanceToDest < 100 {
let current_date_time = Date()
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .medium
formatter.dateFormat = "HH:mm dd MMM yyyy"
formatter.timeZone = TimeZone.autoupdatingCurrent
let timestamp = formatter.string(from: current_date_time)
if !runOnce {
sendEmail(messageText: "\(contact_name); Employee \(employee_number) Arrived at destination \(destination.coordinate.latitude), \(destination.coordinate.longitude) at \(timestamp)")
}
distanceText = String(distanceToDest)
distanceText = distanceText + " m"
runOnce = true
}
else {
distanceText = String(distanceToDest)
distanceText = distanceText + " m"
}
distanceLabel.text = "Currently \(distanceText) from destination"
lbl_location.text = String(mylat) + ", " + String(mylong)
uploadData()
}
@IBAction func uploadData()
{
var httprequest = URLRequest(url: dburl! as URL)
httprequest.httpMethod = "POST"
var dataString = "####" // starting POST string with a secretWord
// the POST string has entries separated by &
dataString = dataString + "&unique_id=" + unique_id
dataString = dataString + "&mylat=" + String(mylat) // add items as name and value
dataString = dataString + "&mylong=" + String(mylong)
dataString = dataString + "&contact_name=" + contact_name
dataString = dataString + "&contact_number=" + contact_number
dataString = dataString + "&employee_number=" + employee_number
let current_date_time = Date()
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .medium
formatter.dateFormat = "HH:mm dd MMM yyyy"
formatter.timeZone = TimeZone.autoupdatingCurrent
let timestamp = formatter.string(from: current_date_time)
dataString = dataString + "×tamp=" + timestamp
// convert the post string to utf8 format
let dataD = dataString.data(using: .utf8) // convert to utf8 string
do
{
// the upload task, uploadJob, is defined here
let uploadJob = URLSession.shared.uploadTask(with: httprequest, from: dataD!)
//let uploadJob = URLSession.shared.uploadTask(with: httprequest, from: dataD)
uploadJob.resume()
}
}
override func viewDidLoad() {
super.viewDidLoad()
location_process.delegate = self
location_process.pausesLocationUpdatesAutomatically = false
location_process.desiredAccuracy = kCLLocationAccuracyBest
location_process.requestWhenInUseAuthorization()
location_process.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled()
{
location_process.distanceFilter = 50
location_process.allowsBackgroundLocationUpdates = true
location_process.startUpdatingLocation()
}
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}