AppDelegate中的EXC_BAD_ACCESS

时间:2018-10-09 21:33:22

标签: swift exc-bad-access

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

我遇到了一个似乎无法解决的令人讨厌的问题。

在应用程序的“主页”中,有一个用户可以点击以进入另一个视图控制器的视图。问题是,新的视图控制器似乎会使应用程序在viewWillAppear()之后崩溃。

从“首页”:

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 {

我已经尝试过启用僵尸,但是没有运气。内存配置文件仅在崩溃标志处显示“未知”。

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

非常感谢您!

1 个答案:

答案 0 :(得分:0)

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