在我的应用程序中按下后退按钮时,将调用一个委托。所有功能均正常运行,但是视图转换之间存在较大的延迟。这使用户感到该应用程序已崩溃或无法正常运行。
此处显示了触发并导致延迟的委托方法。
func setValues(obj: overviewProps){
overview = obj
print("Values Set")
surveyToEdit.overview = obj
updateSurveyObj()
}
func setArray(obj: [areaProps]) {
areas = obj
print("Set Array Sucessfully")
print(areas.count)
surveyToEdit.areas = obj
updateSurveyObj()
}
func saveSurveys(x : addedSurveys){
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(x, toFile: addedSurveys.ArchiveURL.path)
if isSuccessfulSave {
os_log("Surveys Saved", log: OSLog.default, type: .debug)
} else {
os_log("Failed to Save", log: OSLog.default, type: .error)
}
}
func updateSurveyObj(){
//this must be the issue
surveyToEdit.name = surveyToEdit.overview?.name ?? surveyToEdit.name
surveyToEdit.areas! = areas
surveyToEdit.overview! = overview
surveyListSurveys.surveys[surveyIndex] = surveyToEdit
saveSurveys(x: surveyListSurveys)
delegate?.saveInfo(updatedArray: surveyListSurveys)
print("the name is : \(surveyToEdit.overview!.name)")
}
连接到调试器后,在此延迟期间,设备显示较高的内存使用率和较高的CPU使用率。
这是项目概述的整个视图控制器:
import UIKit
导入os.log
class projectOverview:UIViewController,overviewDelegate,AreaDelegate {
var surveyToEdit = survey(areas: nil, settings: nil, overview: nil, name: "New Survey")
let IMG = UIImage(named: "noImage.png")
var areas = [areaProps(x: "New Area", y: "", z: "", a: "", b: "", p1: nil, p2: nil, p3: nil), areaProps(x: "New Area", y: "", z: "", a: "", b: "", p1: nil, p2: nil, p3: nil)]
var settings = companySettings(manager: "")
var overview = overviewProps(x: "", y: "", a: "", b: "", c: "", d: "", e: "", f: "", st: "", ct: "", stt: "")
var projectTitle: String = ""
let areaView = areaDetails()
let surveyHomeScreen = surveyList()
var surveyIndex = 0
var surveyListSurveys = addedSurveys(survey: [survey]())
var delegate:projectDelegate?
// need this to eventually pass back the data in the the survey object
func saveSurveys(x : addedSurveys){
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(x, toFile: addedSurveys.ArchiveURL.path)
if isSuccessfulSave {
os_log("Surveys Saved", log: OSLog.default, type: .debug)
} else {
os_log("Failed to Save", log: OSLog.default, type: .error)
}
}
func setProjDetails(){
print("gay")
if surveyToEdit.areas != nil{
areas = surveyToEdit.areas!
print("There is area")
}
if surveyToEdit.overview != nil{
overview = surveyToEdit.overview!
print("there is overview")
}
}
func updateSurveyObj(){
//this must be the issue
surveyToEdit.name = surveyToEdit.overview?.name ?? surveyToEdit.name
surveyToEdit.areas! = areas
surveyToEdit.overview! = overview
surveyListSurveys.surveys[surveyIndex] = surveyToEdit
saveSurveys(x: surveyListSurveys)
delegate?.saveInfo(updatedArray: surveyListSurveys)
print("the name is : \(surveyToEdit.overview!.name)")
}
let overviewController = addOverview()
override func viewDidLoad() {
super.viewDidLoad()
//areas = surveyToEdit.areas!
//overview = surveyToEdit.overview!
// Do any additional setup after loading the view.
//overviewController.delegate = self
//areaView.delegate = self
}
// MARK : - Actions
@IBAction func showPDF(_ sender: Any) {
performSegue(withIdentifier: "goToShare", sender: self)
print(overview.name)
}
@IBAction func editAreas(_ sender: Any) {
performSegue(withIdentifier: "goToServiceArea", sender: self)
}
@IBAction func editOverView(_ sender: Any) {
performSegue(withIdentifier: "goToMainDetails", sender: self)
}
func setValues(obj: overviewProps){
overview = obj
print("Values Set")
surveyToEdit.overview = obj
updateSurveyObj()
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToMainDetails"{
let vc = segue.destination as! addOverview
vc.overview = overview
vc.delegate = self
} else if segue.identifier == "goToServiceArea"{ // this is the segue
let vc = segue.destination as! areaDetails
vc.areaArray = self.areas
vc.delegate = self
print(" \(areas.count) Areas in Array ") // the array contains only 1 object
} else {
let vc = segue.destination as! shareReport
vc.surveyToDisplay = surveyToEdit
}
}
override func willMove(toParent parent: UIViewController?) {
super.willMove(toParent:parent)
if parent == nil {
updateSurveyObj()
}
}
func setArray(obj: [areaProps]) {
areas = obj
print("Set Array Sucessfully")
print(areas.count)
surveyToEdit.areas = obj
updateSurveyObj()
}
@IBOutlet var floorPlanIMG: UIImageView!
这是按下后退按钮的视图:
import UIKit
class addOverview:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
var overview = overviewProps(x:“”,y:“”,a:“,b:”“,c:”“,d:”“,e:”“,f:”“,st: “”,ct:“”,stt:“”)
// MARK: - Init all UIVariables
let buildingTypes = ["Industrial", "School", "Office", "Multi-Tenant", "Church", "Manufacturing", "Misc"]
let cleaningFreqs = ["1/Week", "2/Week", "3/Week", "4/Week", "5/Week", "6/Week", "7/Week", "1/EOW", "1/Month"]
@IBOutlet var datePicker: UIDatePicker!
@IBOutlet var buildingName: UITextField!
@IBOutlet var street: UITextField!
@IBOutlet var city: UITextField!
@IBOutlet var State: UITextField!
@IBOutlet var contactName: UITextField!
@IBOutlet var phoneNumber: UITextField!
@IBOutlet var email: UITextField!
@IBOutlet var buildingPicker: UIPickerView!
@IBOutlet var cleaningFreqPicker: UIPickerView!
@IBOutlet var sqFt: UITextField!
var delegate:overviewDelegate?
// MARK: - Conform to PickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 2
{
return buildingTypes.count
}
else{
return cleaningFreqs.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 2)
{
return buildingTypes[row]
}else{
return cleaningFreqs[row]
}
}
override func viewDidLoad() {
super.viewDidLoad()
updateView()
buildingPicker.delegate = self
cleaningFreqPicker.delegate = self
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
if overview.building != ""{
buildingPicker.selectRow(buildingTypes.index(of: overview.building)!, inComponent: 0, animated: false)
cleaningFreqPicker.selectRow(cleaningFreqs.index(of: overview.freq)!, inComponent: 0, animated: false)
}
}
// set all the values of the object
func updateView(){
buildingName.text = overview.name
city.text = overview.city
contactName.text = overview.contact
email.text = overview.email
phoneNumber.text = overview.phone
sqFt.text = overview.sft
State.text = overview.state
street.text = overview.street
}
func setValues(){
overview.name = buildingName.text!
overview.city = city.text!
overview.contact = contactName.text!
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
overview.date = dateFormatter.string(from: datePicker.date)
overview.email = email.text!
overview.building = buildingTypes[buildingPicker.selectedRow(inComponent: 0)]
overview.freq = cleaningFreqs[cleaningFreqPicker.selectedRow(inComponent: 0)]
overview.phone = phoneNumber.text!
overview.sft = sqFt.text!
overview.state = State.text!
overview.street = street.text!
}
// MARK: - Navigation
override func willMove(toParent parent: UIViewController?) {
super.willMove(toParent:parent)
if parent == nil {
setValues()
print("Back Button Pressed")
delegate?.setValues(obj: overview)
}
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
}