addTarget选择器函数意外不起作用

时间:2018-04-03 15:25:02

标签: swift uibutton viewcontroller addtarget

import UIKit
import Mapbox
import QRCode

class QRCodeViewController: UIViewController {
    var id: String? {
        didSet {
            let qrCode = QRCode(id!)
            qrCodeImageView.image = qrCode?.image
        }
    }

    var qrCodePortraitContraintsArray = [NSLayoutConstraint]()
    var qrCodeLandscapetContraintsArray = [NSLayoutConstraint]()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.tintColor = .white
        self.navigationController?.navigationBar.barTintColor = .gray
        view.addSubview(qrCodeImageView)

        NSLayoutConstraint.activate([qrCodeImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor), qrCodeImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor)])

        qrCodePortraitContraintsArray = [qrCodeImageView.heightAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.7), qrCodeImageView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.7)]
        qrCodeLandscapetContraintsArray = [qrCodeImageView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.7), qrCodeImageView.widthAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.7)]

        checkAndAdjustContraintst()

        view.addSubview(closeButton)
        view.addSubview(shareButton)

        NSLayoutConstraint.activate([closeButton.topAnchor.constraint(equalTo: qrCodeImageView.bottomAnchor), closeButton.rightAnchor.constraint(equalTo: qrCodeImageView.rightAnchor), closeButton.widthAnchor.constraint(equalTo: qrCodeImageView.widthAnchor, multiplier: 0.5), closeButton.heightAnchor.constraint(equalToConstant: 40)])

        NSLayoutConstraint.activate([shareButton.topAnchor.constraint(equalTo: qrCodeImageView.bottomAnchor), shareButton.widthAnchor.constraint(equalTo: qrCodeImageView.widthAnchor, multiplier: 0.5), shareButton.leftAnchor.constraint(equalTo: qrCodeImageView.leftAnchor), shareButton.heightAnchor.constraint(equalToConstant: 40)])
    }

    var shareButton: UIButton = {
        var cb = UIButton()
        cb.translatesAutoresizingMaskIntoConstraints = false
        cb.setTitle("Share", for: .normal)
        cb.backgroundColor = .blue
        cb.addTarget(self, action: #selector(openShareController), for: .touchUpInside)
        return cb
    }()

    @objc fileprivate func openShareController() {
        print("Showing")
    }

    var closeButton: CustomUIButton = {
        let cb = CustomUIButton()
        cb.setTitle("Close", for: .normal)
        cb.addTarget(self, action: #selector(closeView(_:)), for: .touchUpInside)
        return cb
    }()

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        checkAndAdjustContraintst()
    }

    fileprivate func checkAndAdjustContraintst() {
        if UIDevice.current.orientation.isLandscape {
            NSLayoutConstraint.activate(qrCodeLandscapetContraintsArray)
            NSLayoutConstraint.deactivate(qrCodePortraitContraintsArray)
        }
        else {
            NSLayoutConstraint.activate(qrCodePortraitContraintsArray)
            NSLayoutConstraint.deactivate(qrCodeLandscapetContraintsArray)
        }
    }

    var qrCodeImageView: UIImageView = {
        let civ = UIImageView()
        civ.image = UIImage(named: "contact")
        civ.translatesAutoresizingMaskIntoConstraints = false
        civ.clipsToBounds = true
        civ.backgroundColor = .white
        civ.isUserInteractionEnabled = true
        civ.contentMode = .scaleAspectFit
        return civ
    }()

    var containerView: UIView = {
        let cv = UIView()
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.clipsToBounds = true
        cv.backgroundColor = .white
        return cv
    }()

    func setupObservers() {
        NotificationCenter.default.addObserver(self, selector: #selector(removePopViewController), name: NSNotification.Name.init("didSelectADifferentTab"), object: nil)
    }

    @objc func removePopViewController() {
        self.dismiss(animated: true, completion: nil)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        qrCodeImageView.addShadow()
        closeButton.addShadow()
        shareButton.addShadow()
    }
}

以下是包含UIImageView的ViewController的代码。我试图调用openShareController函数。我已经尝试了几乎所有的功能但按下按钮时没有调用该功能。我之前已经在项目中完成了这个并且同样的过程也起作用。我错过了什么吗?

如果您需要任何其他代码,请与我们联系。

0 个答案:

没有答案