0

In our checkout process, we have an existing UIButton for submitting an order. As we are starting to support Apple Pay, we want to recycle the existing button and show the Apple Pay option.

Within storyboard, we have placed a UIButton and connected it to our UIViewController class. Instead of initializing another button specifically for PKPaymentButton, I want to do something like:

submit_order_btn = PKPaymentButton(paymentButtonType: .buy, paymentButtonStyle: .black)

However, this is not working and throwing a warning: Instance will be immediately deallocated because property 'submit_order_btn' is 'weak'.

As we have some basic validation checking happening when the button is pressed, I would like to simply update the button styling itself without affecting any associated actions with the button.

I have also created the PKPaymentButton and added that to the existing button as a subview but it breaks the button functionality for other workflows and I have to track the subviews now to pop/push into the UIButton.

apple_pay_button = PKPaymentButton(paymentButtonType: .inStore, paymentButtonStyle: .black)
            apple_pay_button.frame = CGRect.init(x: 10, y: 10, width: submit_order_btn.frame.width-20, height: submit_order_btn.frame.height-20)
            submit_order_btn.addSubview(apple_pay_button)

I am hoping there is a better way of doing this where I can simply update the UIButton styling to match the PKPaymentButton. I don't want to recreate the button manually as well because it is against Apple's recommendations.

whawhat
  • 175
  • 2
  • 7
  • You can't assign a new button instance to an outlet in code and expect it to affect what is on screen; the new button isn't part of the view hierarchy. You need to add it as a sub view. Because you don't do that and the outlet is weak it will be deallocated immediately as per the warning.it wouldn't make sense to add the Apple Pay button as a sub view of an existing button. It needs to be a separate button in the same view that contains your existing submit order button. You would typically have two buttons e.g. "checkout" and "checkout with Apple Pay" – Paulw11 Oct 22 '20 at 20:27
  • Thanks, I took your advice. Resolving this issue now. – whawhat Oct 25 '20 at 20:44

0 Answers0