EXC_BAD_ACCESS 在 AppDelegate 中

EXC_BAD_ACCESS in AppDelegate

我是一名 Android 开发人员,我只做一些副业 iOS 编程,所以请原谅我是一个初学者。

我遇到了一个我似乎无法解决的棘手问题。

在应用程序的 "homepage" 中,有一个视图,用户可以点击该视图以转到另一个视图控制器。问题是,新的视图控制器似乎使应用程序在 viewWillAppear() 之后崩溃。

来自"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)
    }
}

代码如下:

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()
}

我已经尝试清空 viewDidLoad 函数,但仍然无法正常工作。

日志仅停在:

viewDidLoad
viewDidLoad finished
viewWillAppear
viewWillAppear finished
(lldb) 

并抛出这个异常:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x2a26849f8)

同时指向

class AppDelegate: UIResponder, UIApplicationDelegate {

我已经尝试启用僵尸,但没有成功。内存分析仅在崩溃标志处显示 "Unknown"。

我真的很困惑。对我有什么提示吗?

非常感谢您!

我的问题是导致整个应用程序崩溃的复杂视图。如果您遇到类似的事情,请尝试在另一个项目中重建视图,然后一个接一个地删除。 :) 感谢@OOPer 的帮助!