内存泄漏与init(coder :)

时间:2018-06-26 09:04:34

标签: ios swift memory-leaks xcode-instruments

这是 SignInViewController

import UIKit
import Material
import SwiftyUserDefaults
import Alamofire

/// Protocol containing the functions to be called in the coordinator.
protocol SignInViewControllerDelegate: class {
    func loginButtonTapped(controller: SignInViewController)
    func setRootTabController()
}

/// Class for handling Login screen
class SignInViewController: UIViewController {

    // IBOutlets
    @IBOutlet weak var imageViewLogo: UIImageView!
    @IBOutlet weak var labelMaro: UILabel!
    @IBOutlet weak var buttonLogin: UIButton!
    @IBOutlet weak var constraint_top_imageLogo: NSLayoutConstraint!
    @IBOutlet weak var constraint_top_label: NSLayoutConstraint!
    @IBOutlet weak var constraint_bottom_textFieldPassword: NSLayoutConstraint!

    @IBOutlet weak var textFieldEmail :BindingTextField! {
        didSet {
            textFieldEmail.bind { self.viewModel.email = $0 }
        }
    }

    @IBOutlet weak var textFieldPassword :BindingTextField! {
        didSet {
            textFieldPassword.bind { self.viewModel.password = $0 }
        }
    }

    @IBOutlet weak var viewModel: SignInViewModel!

    // Variables
    public weak var delegate: SignInViewControllerDelegate?
    var isAnimated = false

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUI()
    }

    /// Set initial UI settings
    func setUI() {
        self.textFieldEmail.backgroundColor = Colors.kColorTextFieldBackground
        self.textFieldPassword.backgroundColor = Colors.kColorTextFieldBackground
        self.textFieldEmail.delegate = self
        self.textFieldPassword.delegate = self
        self.view.isUserInteractionEnabled = true
        let tapGestureDismissKeyboard = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboardOnViewTap(_:)))
        self.view.addGestureRecognizer(tapGestureDismissKeyboard)
    }

    /// Called when the parent view of this view controller is touched. Used for dismissing the keyboard appearing from either email or password text fields
    ///
    /// - Parameter genture: UITapGestureRecognizer instance
    @objc func dismissKeyboardOnViewTap(_ genture: UITapGestureRecognizer) {
        self.textFieldEmail.resignFirstResponder()
        self.textFieldPassword.resignFirstResponder()
    }

    /// Login button action
    ///
    /// - Parameter sender: Login button
    @IBAction func login(sender: UIButton) {
        self.login()
    }

    /// Login functionality
    func login() {
        if !self.isAnimated {
            self.isAnimated = true
            UIView.animate(withDuration: 0.8, animations: {
                self.constraint_top_imageLogo.constant = self.constraint_top_imageLogo.constant - 100
                self.view.layoutIfNeeded()
            }, completion: { _ in
                UIView.animate(withDuration: 3, animations: {
                    self.textFieldEmail.isHidden = false
                    self.textFieldPassword.isHidden = false
                    let buttonY = self.buttonLogin.frame.origin.y
                    let labelBottomPosition = self.labelMaro.frame.origin.y + self.labelMaro.frame.size.height
                    let totalHeight = buttonY - labelBottomPosition
                    let heightToBeEquallyDivided = (totalHeight - 120)/2
                    self.constraint_bottom_textFieldPassword.constant = heightToBeEquallyDivided
                }, completion: {_ in
                })
            })
        } else {
            if self.viewModel.checkValidations(view: self.view) && self.viewModel.isConnectedToInternet(view: self.view) {
                Defaults[.isLoggedIn] = true
                self.delegate?.setRootTabController()
            }
        }
    }
}

/// Extension required when working on Storyboard + Coordinator.
extension SignInViewController: Storyboarded {

}

/// Extension for ErrorTextField delegate
extension SignInViewController: TextFieldDelegate {
    public func textFieldDidEndEditing(_ textField: UITextField) {
        (textField as? ErrorTextField)?.isErrorRevealed = false
    }

    public func textFieldShouldClear(_ textField: UITextField) -> Bool {
        (textField as? ErrorTextField)?.isErrorRevealed = false
        return true
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        (textField as? ErrorTextField)?.isErrorRevealed = true
        if textField == self.textFieldEmail {
            self.textFieldPassword.becomeFirstResponder()
        } else {
            textField.resignFirstResponder()
            self.login()
        }

        return true
    }
}  

我执行了内存泄漏检查,这就是我得到的:

enter image description here

这是在执行任何操作之前或说刚启动之后。无法真正弄清楚泄漏的原因。

0 个答案:

没有答案