【问题标题】:Xcode ViewController shows Cannot find 'pay' in scopeXcode ViewController 显示在范围内找不到“支付”
【发布时间】:2022-01-07 04:37:45
【问题描述】:

我正在尝试将我的 iOS 应用程序链接到条带,但在我的视图控制器中它显示错误 - “在范围内找不到‘支付’”

附上错误截图——

我的 ViewController 是 -

  import UIKit
  import Stripe
  import Alamofire

  class ViewController: UIViewController {
 // Mark  UIViews
  var productStackView = UIStackView()
  var paymentStackView = UIStackView()
  var paymentTextField =  STPPaymentCardTextField()
  var productImageView = UIImageView()
  var productLabel = UILabel()
  var payButton = UIButton()
  var loadingSpinner = UIActivityIndicatorView()
  var outputTextView = UITextView()
   override func viewDidLoad() {
    super.viewDidLoad()
    self.setupUI()
  }
func setupUI() {
    setupProductImage()
    setupProductLabel()
    setupLoadingSpinner()
    setupPaymentTextFiled()
    setupPayButton()
    setupOutputTextView()
    
    self.productStackView.frame = CGRect(x: 0, y: 70, width: 330, height: 150)
    self.productStackView.center.x = self.view.center.x
    self.productStackView.alignment = .center
    self.productStackView.axis = .vertical
    self.productStackView.distribution = .equalSpacing
    self.productStackView.addArrangedSubview(self.productImageView)
    self.productStackView.setCustomSpacing(10, after: self.productImageView)
    self.productStackView.addArrangedSubview(self.productLabel)
    self.view.addSubview(self.productStackView)

    self.paymentStackView.frame = CGRect(x:0, y: 250, width: 300, height: 100)
    self.paymentStackView.center.x = self.view.center.x
    self.paymentStackView.alignment = .fill
    self.paymentStackView.axis = .vertical
    self.paymentStackView.distribution = .equalSpacing
    self.paymentStackView.addArrangedSubview(self.paymentTextField)
    self.paymentStackView.addArrangedSubview(self.payButton)
    self.view.addSubview(self.paymentStackView)

   }
   func setupProductImage(){
    self.productImageView = UIImageView(frame: CGRect(x: 50, y: 50, width: 275, height: 200))
    self.productImageView.image = UIImage(named: "stripe press")
    self.productImageView.contentMode = .scaleAspectFit
   }
   func  setupProductLabel() {
    self.productLabel.frame = CGRect(x: 0, y: 420, width: self.view.frame.width, height: 50)
   }


  func setupOutputTextView() {
   self.outputTextView.frame = CGRect(x: 0, y: 420, width: self.view.frame.width - 50, height: 100)
    self.outputTextView.center.x = self.view.center.x
    self.outputTextView.textAlignment = .left
    self.outputTextView.font = UIFont.systemFont(ofSize: 18)
    self.outputTextView.text = ""
    self.outputTextView.layer.borderColor = UIColor.purple.cgColor
    self.outputTextView.isEditable = false
    self.view.addSubview(self.outputTextView)
   }
    func  setupPaymentTextFiled() {
    self.paymentTextField.frame = CGRect(x: 0, y: 0, width: 330, height: 660)
   }
   func setupPayButton() {
    self.payButton.frame = CGRect(x: 60, y: 480, width: 150, height: 40)
    self.payButton.setTitle("Submit Payment", for: .normal)
    self.payButton.setTitleColor(UIColor.white, for: .normal)
    self.payButton.layer.cornerRadius = 5.0
    self.payButton.backgroundColor = UIColor.init(red:50/255,green: 50/255,blue: 93/255, alpha:1.0)
    self.payButton.layer.borderWidth = 1.0
    self.payButton.addTarget(self, action: #selector(pay), for: .touchUpInside)
  }***/* ERROR -Cannot find 'pay' in scope */***
   func setupLoadingSpinner() {
    self.loadingSpinner.color = UIColor.darkGray
    self.loadingSpinner.frame = CGRect(x: 0, y: 300, width: 25, height: 25)
    self.loadingSpinner.center.x = self.view.center.x
    self.view.addSubview(self.loadingSpinner)
   }

   func startLoading() {
    DispatchQueue.main.async {
        self.loadingSpinner.startAnimating()
        self.loadingSpinner.isHidden = false
      }
   }

    func stopLoading() {
    DispatchQueue.main.async {
        self.loadingSpinner.stopAnimating()
        self.loadingSpinner.isHidden = true
      }
   }
    func diplayStatus (_ message:String) {
    DispatchQueue.main.async {
       self.outputTextView.text += message + " \n"
           self.outputTextView.scrollRangeToVisible(NSMakeRange(self.outputTextView.text.count - 1, 1))
        }
     }

  }

上面代码中的错误我加粗了。

如前所述,我还附上了截图。 我尝试了一切,但无法解决它。如何整理?

【问题讨论】:

  • 什么是“支付”?我也无法在您的代码中找到它。 “支付”应该是能够创建选择器的有效函数。

标签: ios swift xcode stripe-payments


【解决方案1】:

选择器应该是您希望在用户点击支付按钮时运行的 Objc 函数。

@objc func pay() {
    //Your pay code goes in here
}

【讨论】:

  • 如何将它集成到我的代码中?
  • 你关注过 Stripe 文档吗?我不知道它是如何工作的,但是您肯定需要导入库并遵循他们的文档,然后您的 pay 方法应该使用 stripe 的 API 来进行付款。
  • 请看下面我的回答
  • 为你点赞
【解决方案2】:

是的,你们是对的,我添加了“@objc func pay() {}”,如下所示,一切正常:-

      .......func diplayStatus (_ message:String) {
    DispatchQueue.main.async {
       self.outputTextView.text += message + " \n"
             self.outputTextView.scrollRangeToVisible(NSMakeRange(self.outputTextView.text.count - 1, 1))
    }

}
@objc func pay() {} //here
}

【讨论】:

  • 所以你决定用已经给出的相同代码添加一个新答案?
猜你喜欢
  • 2021-11-29
  • 2021-09-17
  • 2021-08-19
  • 2021-07-12
  • 2021-09-16
  • 2020-11-23
  • 2021-06-18
  • 2021-01-15
相关资源
最近更新 更多