2016-11-15 60 views
1

我試圖使用Fabric設置Stripe,我的第一步當然是接受來自用戶的信用卡信息。通過下面的代碼,提交按鈕變爲啓用時,但應用程序崩潰時,單擊按鈕。錯誤消息是「終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:' - [Stripe3.ViewController submitCard:]:無法識別的選擇器發送到實例0x7fe83740dbd0'」Stripe Fabric iOS集成

我將不勝感激任何見解!

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) 
    } 
} 

回答

0

確保你已經正確連接按鈕故事板的動作向右IBAction爲在正確的UIViewController子類。有時候,這只是Storyboard場景/ UI組件複製/粘貼錯誤的問題。