I'm an Android developer, and i only do some side-gig iOS programming, so please forgive me for being quite a beginner.
I'm encountering a nasty issue i can't seem to solve.
When in the "homepage" of the app, there's a view the user can tap to get to another view controller. Problem is, the new view controller seems to make the app crash just after viewWillAppear().
From the "homepage":
let tap_datiutente = UITapGestureRecognizer(target: self, action: #selector(self.func_tap_datiutente(_:)))
view_datiutente.addGestureRecognizer(tap_datiutente)
[...]
@objc func func_tap_datiutente (_ sender : UITapGestureRecognizer) {
if pref_login?.integer(forKey: Costanti.sp_stato_login) == Costanti.sp_int_visitatore {
// Just an alert for non-registered users, who can't access this view //
Operatore.MostraAlert(viewController: self, titolo: "Attenzione", testo: "Questa funzione è riservata agli utenti già registrati.")
} else {
// This is the action performed to reach the view controller //
self.performSegue(withIdentifier: "seg_home_profilo", sender: self)
}
}
Here's the code:
import UIKit
import Alamofire
class DatiUtente: UIViewController, UITextFieldDelegate {
@IBOutlet weak var scroll_view: UIScrollView!
@IBOutlet weak var view_1: UIView!
@IBOutlet weak var tf_nome_fatt: UITextField!
@IBOutlet weak var tf_cognome_fatt: UITextField!
@IBOutlet weak var tf_indirizzo_fatt: UITextField!
@IBOutlet weak var tf_numciv_fatt: UITextField!
@IBOutlet weak var tf_cap_fatt: UITextField!
@IBOutlet weak var tf_comune_fatt: UITextField!
@IBOutlet weak var tf_provincia_fatt: UITextField!
@IBOutlet weak var tf_cod_fisc_fatt: UITextField!
@IBOutlet weak var tf_piva_fatt: UITextField!
@IBOutlet var constr_1: NSLayoutConstraint!
@IBOutlet weak var view_2: UIView!
@IBOutlet weak var tf_nome_allievo: UITextField!
@IBOutlet weak var tf_cognome_allievo: UITextField!
@IBOutlet weak var tf_indirizzo_allievo: UITextField!
@IBOutlet weak var tf_cap_allievo: UITextField!
@IBOutlet weak var tf_comune_allievo: UITextField!
@IBOutlet weak var tf_provincia_allievo: UITextField!
@IBOutlet weak var tf_numciv_allievo: UITextField!
@IBOutlet weak var tf_codfisc_allievo: UITextField!
@IBOutlet weak var tf_numcell_allievo: UITextField!
@IBOutlet weak var tf_email_allievo: UITextField!
@IBOutlet weak var view_privacy_1: UIView!
@IBOutlet weak var switch_privacy_1: UISwitch!
@IBOutlet weak var view_privacy_2: UIView!
@IBOutlet weak var switch_privacy_2: UISwitch!
@IBOutlet var constr_2: NSLayoutConstraint!
/* METODI DI OVERRIDE */
override func viewDidLoad() {
print("viewDidLoad")
super.viewDidLoad()
let pref_login = UserDefaults(suiteName: Costanti.SP_LOGIN)
tf_nome_fatt.text = pref_login?.string(forKey: "nomefatt")
tf_cognome_fatt.text = pref_login?.string(forKey: "cognomefatt")
tf_indirizzo_fatt.text = pref_login?.string(forKey: "indirfatt")
tf_numciv_fatt.text = pref_login?.string(forKey: "numcivfatt")
tf_cap_fatt.text = pref_login?.string(forKey: "capfatt")
tf_comune_fatt.text = pref_login?.string(forKey: "comunefatt")
tf_provincia_fatt.text = pref_login?.string(forKey: "provfatt")
tf_cod_fisc_fatt.text = pref_login?.string(forKey: "codfiscfatt")
tf_piva_fatt.text = pref_login?.string(forKey: "pivafatt")
tf_nome_allievo.text = pref_login?.string(forKey: "nome")
tf_cognome_allievo.text = pref_login?.string(forKey: "cognome")
tf_indirizzo_allievo.text = pref_login?.string(forKey: "indirizzo")
tf_numciv_allievo.text = pref_login?.string(forKey: "numciv")
tf_cap_allievo.text = pref_login?.string(forKey: "cap")
tf_comune_allievo.text = pref_login?.string(forKey: "comune")
tf_provincia_allievo.text = pref_login?.string(forKey: "prov")
tf_codfisc_allievo.text = pref_login?.string(forKey: "codfisc")
tf_numcell_allievo.text = pref_login?.string(forKey: "cell")
tf_email_allievo.text = pref_login?.string(forKey: "email")
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard (_:)))
tapGesture.cancelsTouchesInView = false
self.view.addGestureRecognizer(tapGesture)
let tap_privacy_1 = UITapGestureRecognizer(target: self, action: #selector(self.privacy(_:)))
view_privacy_1.addGestureRecognizer(tap_privacy_1)
let tap_privacy_2 = UITapGestureRecognizer(target: self, action: #selector(self.privacypolicy(_:)))
view_privacy_2.addGestureRecognizer(tap_privacy_2)
scroll_view.delaysContentTouches = false
print("viewDidLoad finished")
}
override func viewWillAppear(_ animated: Bool) {
print("viewWillAppear")
super.viewWillAppear(animated)
print("viewWillAppear finished")
}
override func viewDidAppear(_ animated: Bool) {
print("viewDidAppear")
super.viewDidAppear(animated)
Mostra_View_1()
print("viewDidAppear finished")
}
}
[...]
private func Mostra_View_1() {
view_1.isHidden = false
view_2.isHidden = true
HideKeyboard()
constr_1.constant = 25
constr_1.isActive = true
constr_2.isActive = false
scroll_view.scrollToTop()
}
private func Mostra_View_2() {
view_2.isHidden = false
view_1.isHidden = true
HideKeyboard()
constr_2.constant = 25
constr_1.isActive = false
constr_2.isActive = true
scroll_view.scrollToTop()
}
private func HideKeyboard() {
tf_nome_fatt.resignFirstResponder()
tf_cognome_fatt.resignFirstResponder()
tf_indirizzo_fatt.resignFirstResponder()
tf_numciv_fatt.resignFirstResponder()
tf_cap_fatt.resignFirstResponder()
tf_comune_fatt.resignFirstResponder()
tf_provincia_fatt.resignFirstResponder()
tf_cod_fisc_fatt.resignFirstResponder()
tf_piva_fatt.resignFirstResponder()
tf_nome_allievo.resignFirstResponder()
tf_cognome_allievo.resignFirstResponder()
tf_indirizzo_allievo.resignFirstResponder()
tf_numciv_allievo.resignFirstResponder()
tf_cap_allievo.resignFirstResponder()
tf_comune_allievo.resignFirstResponder()
tf_provincia_allievo.resignFirstResponder()
tf_codfisc_allievo.resignFirstResponder()
tf_numcell_allievo.resignFirstResponder()
tf_email_allievo.resignFirstResponder()
}
I've already tried emptying the viewDidLoad function, but it's still not working.
The log simply stops at:
viewDidLoad
viewDidLoad finished
viewWillAppear
viewWillAppear finished
(lldb)
and throws this exception:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x2a26849f8)
while pointing at
class AppDelegate: UIResponder, UIApplicationDelegate {
I've already tried enabling zombies, but with no luck. The memory profiling just shows "Unknown" at the flag of the crash.
I'm really confused. Got any hint for me?
Thank you very much in advance!
My issue was a complicated view that made the whole app crash. Should you encouter something similar, try to rebuild the view in another project, then remove one piece after the other. :) Thanks to @OOPer for the help!