Не удалось получить доступ к пользовательскому представлению из UIViewController с помощью Swift - PullRequest
0 голосов
/ 11 мая 2018

Я работаю сканер кредитных карт, используя пользовательский вид. Я попробовал приведенный ниже код и не смог получить доступ к пользовательскому классу. Не могли бы вы, пожалуйста, кто-нибудь помочь мне решить эту проблему. Прикрепленный скриншот ошибки ниже. Спасибо ...

let BUTTON_SIZE = CGSize(width: 100, height: 20)
let BUTTON_BUFFER = 10

protocol GymCreditCardScannerViewDelegate: class {
    func creditCardScannerDidCancel(_ scanner: Any?)
    func creditCardScanner(_ scanner: Any?, scannedNumber number: String?)
}

import UIKit

class GymCreditCardScanner: UIView, CardIOViewDelegate {

    weak var scannerdelegate: GymCreditCardScannerViewDelegate?

    var cardIOView: CardIOView?
    var cancelButton: UIButton?
    var doneButton: UIButton?
    var number = ""
    var screenFrame = CGRect.zero
    var innerFrame = CGRect.zero

    func initIntoView(_ superView: UIView?, withDelegate delegate: Any?) -> Any? {

        let frame = CGRect(x: 0, y: 0, width: superView?.frame.size.width ?? 0.0, height: superView?.frame.size.height ?? 0.0)

        self.scannerdelegate = delegate as? GymCreditCardScannerViewDelegate
        screenFrame = frame
        let MARGIN: CGFloat = 0.05
        innerFrame = CGRect(x: frame.size.width * MARGIN, y: frame.size.height * MARGIN, width: frame.size.width * (1 - 2 * MARGIN), height: frame.size.height * (1 - 2 * MARGIN))
        cardIOView = CardIOView(frame: innerFrame)
        cardIOView?.delegate = self as CardIOViewDelegate
        cardIOView?.hideCardIOLogo = true
        cardIOView?.scannedImageDuration = 0.1
        addSubview(cardIOView!)
        let buttonSize: CGSize = BUTTON_SIZE
        cancelButton = UIButton(frame: CGRect(x: frame.size.width / 2 - buttonSize.width / 2, y: innerFrame.origin.y + innerFrame.size.height - buttonSize.height - CGFloat(BUTTON_BUFFER), width: buttonSize.width, height: buttonSize.height))
        cancelButton?.setTitle("Cancel", for: .normal)
        cancelButton?.addTarget(self, action: Selector(("handleCancelPress:")), for: .touchUpInside)
        addSubview(cancelButton!)
        superView?.addSubview(self)

        return self
    }

    func remove() {
        if (cardIOView != nil) {
            cardIOView?.removeFromSuperview()
        }
        cardIOView = nil
        removeFromSuperview()
    }

    func cardIOView(_ cardIOView: CardIOView!, didScanCard cardInfo: CardIOCreditCardInfo!) {

        number = cardInfo.cardNumber
        UIGraphicsBeginImageContextWithOptions(cardIOView.frame.size, true, 1)
        if let aContext = UIGraphicsGetCurrentContext() {
            cardIOView.layer.render(in: aContext)
        }
        let capture: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        let captureView = UIImageView(image: capture)
        captureView.frame = cardIOView.frame
        addSubview(captureView)
        bringSubview(toFront: cancelButton!)
        cardIOView.removeFromSuperview()
        let frame: CGRect = screenFrame
        let buttonSize: CGSize = BUTTON_SIZE
        doneButton = UIButton(frame: CGRect(x: frame.size.width / 2 - buttonSize.width / 2, y: innerFrame.origin.y + innerFrame.size.height - buttonSize.height - CGFloat(BUTTON_BUFFER), width: buttonSize.width, height: buttonSize.height))
        doneButton?.setTitle("Done", for: .normal)
        doneButton?.addTarget(self, action: Selector(("handleDonePress:")), for: .touchUpInside)
        addSubview(doneButton!)
        doneButton?.alpha = 0

        UIView.animate(withDuration: 0.075, delay: 0, options: .curveEaseOut, animations: {
            self.doneButton?.alpha = 1
            self.doneButton?.frame = CGRect(x: frame.size.width / 2 - buttonSize.width / 2 + (buttonSize.width + CGFloat(BUTTON_BUFFER)) / 2, y: self.innerFrame.origin.y + self.innerFrame.size.height - buttonSize.height - CGFloat(BUTTON_BUFFER), width: buttonSize.width, height: buttonSize.height)
            self.cancelButton?.frame = CGRect(x: frame.size.width / 2 - buttonSize.width / 2 - (buttonSize.width + CGFloat(BUTTON_BUFFER)) / 2, y: self.innerFrame.origin.y + self.innerFrame.size.height - buttonSize.height - CGFloat(BUTTON_BUFFER), width: buttonSize.width, height: buttonSize.height)
        }, completion: nil)
    }

    func handleCancelPress(_ sender: Any?) {
        scannerdelegate?.creditCardScannerDidCancel(self)
    }

    func handleDonePress(_ sender: Any?) {
        scannerdelegate?.creditCardScanner(self, scannedNumber: number)
    }
}

My ViewContoller

class PaymentDetailsVC: UIViewController {

var scanner: GymCreditCardScanner?

//MARK: Credit card scanning
    @IBAction func scanCreditCard(_ sender: Any) {
        cardField.resignFirstResponder()
        scanner = GymCreditCardScanner.initIntoView(view, withDelegate: self)
    }

    func creditCardScannerDidCancel(_ scanner: Any?) {
        scanner.remove()
    }

    func creditCardScanner(_ scanner: Any?, scannedNumber number: String?) {
        scanner.remove()
        scanner = nil
        numberField.text = number
    }
}

error

Ответы [ 3 ]

0 голосов
/ 11 мая 2018

Просто замените var scanner: GymCreditCardScanner? // сканер равен нулю, вы должны его инициализировать

с

lazy var scanner: GymCreditCardScanner = GymCreditCardScanner()
0 голосов
/ 11 мая 2018
  1. Вы написали функцию func initInto(_ superView: UIView?, withDelegate delegate: Any?) -> Any? {, но вместо этого вы пытаетесь вызвать функцию initIntoView.

  2. , которая вместо этого возвращает Any?GymCreditCardScanner.Вы можете привести его так: scanner = GymCreditCardScanner.initInto(view, withDelegate: self) as? GymCreditCardScanner или просто изменить функцию и вернуть GymCreditCardScanner? вместо Any?

  3. func initInto должно быть static func initInto, если вы хотитенравится называть его таким образом или написать свой собственный конструктор для этого представления

  4. Свойство scanner является необязательным (?), поэтому вы должны вызывать функцию remove() следующим образом: scanner?.remove()

0 голосов
/ 11 мая 2018

Ваше объявление метода должно быть таким

static func initInto(_ superView: UIView?, withDelegate delegate: Any?) -> GymCreditCardScanner? {
   //.......

   let frame = CGRect(x: 0, y: 0, width: superView?.frame.size.width ?? 0.0, height: superView?.frame.size.height ?? 0.0)
    let resultCreditCardScanner = GymCreditCardScanner(frame:frame)
    resultCreditCardScanner.scannerdelegate = delegate as? GymCreditCardScannerViewDelegate
    let MARGIN: CGFloat = 0.05
    let innerFrame = CGRect(x: frame.size.width * MARGIN, y: frame.size.height * MARGIN, width: frame.size.width * (1 - 2 * MARGIN), height: frame.size.height * (1 - 2 * MARGIN))
    resultCreditCardScanner.cardIOView = CardIOView(frame: innerFrame)
    resultCreditCardScanner.cardIOView?.delegate = self as CardIOViewDelegate
    resultCreditCardScanner.cardIOView?.hideCardIOLogo = true
    resultCreditCardScanner.cardIOView?.scannedImageDuration = 0.1
    resultCreditCardScanner.addSubview(cardIOView!)
    let buttonSize: CGSize = BUTTON_SIZE
    resultCreditCardScanner.cancelButton = UIButton(frame: CGRect(x: frame.size.width / 2 - buttonSize.width / 2, y: innerFrame.origin.y + innerFrame.size.height - buttonSize.height - CGFloat(BUTTON_BUFFER), width: buttonSize.width, height: buttonSize.height))
    resultCreditCardScanner.cancelButton?.setTitle("Cancel", for: .normal)
    resultCreditCardScanner.cancelButton?.addTarget(self, action: Selector(("handleCancelPress:")), for: .touchUpInside)
    resultCreditCardScanner.addSubview(cancelButton!)
    superView?.addSubview(resultCreditCardScanner)
    return resultCreditCardScanner
}

тогда вы сможете сделать

scanner = GymCreditCardScanner.initInto(view, withDelegate: self)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...