我正在尝试使用 Fabric 设置 Stripe,我的第一步当然是接受用户的信用卡信息。使用下面的代码,“提交”按钮在应该启用时启用,但在单击按钮时应用程序崩溃。错误消息是“由于未捕获的异常‘NSInvalidArgumentException’而终止应用程序,原因:‘-[Stripe3.ViewController submitCard:]:无法识别的选择器发送到实例 0x7fe83740dbd0’”
我将不胜感激任何见解!
class ViewController: UIViewController, STPPaymentCardTextFieldDelegate{
var paymentTextField: STPPaymentCardTextField! = nil
var submitButton: UIButton! = nil
override func viewDidLoad() {
super.viewDidLoad()
paymentTextField = STPPaymentCardTextField(frame: CGRectMake(15, 30, view.frame.width - 30, 44))
paymentTextField.delegate = self
view.addSubview(paymentTextField)
submitButton = UIButton(type: UIButtonType.system)
submitButton.frame = CGRectMake(15, 100, 100, 44)
submitButton.isEnabled = false
submitButton.setTitle("Submit", for: UIControlState.normal)
submitButton.addTarget(self, action: Selector(("submitCard:")), for: UIControlEvents.touchUpInside)
view.addSubview(submitButton)
// Do any additional setup after loading the view, typically from a nib.
}
func paymentCardTextFieldDidChange(_ textField: STPPaymentCardTextField) {
submitButton.isEnabled = textField.valid
}
@IBAction func submitCard(sender: AnyObject?) {
// If you have your own form for getting credit card information, you can construct
// your own STPCardParams from number, month, year, and CVV.
let card = paymentTextField.card!
STPAPIClient.shared().createToken(withCard: card) { token, error in
guard let stripeToken = token else {
NSLog("Error creating token: %@", error!.localizedDescription);
return
}
// TODO: send the token to your server so it can create a charge
let alert = UIAlertController(title: "Welcome to Stripe", message: "Token created: \(stripeToken)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
最佳答案
确保您已将按钮 Storyboard的操作正确链接到正确的 UIViewController 子类中的正确 IBAction。有时这只是 Storyboard 的场景/UI 组件复制/粘贴错误的问题。
关于ios - Stripe 织物 iOS 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40601515/