0

I have a custom UIView controller that I init like so:

inputPhoneNumberView.frame = CGRect(x: 0, y: 0, width: 286, height: 73)
let alert = SAAlertView(title: "Enter Your Phone Number", message: "Enter or update your phone number and we will send you a verification code via SMS.", customView: inputPhoneNumberView)
alert.addAction("Confirm", style: .default, dismissAfterAction: false, hasLoadingIndicator: true) { () -> Void in
 print("Hello!") //Testing code
}
alert.addAction(NSLocalizedString("cancel", comment: ""), style: .cancel, actionBlock: nil)
present(alert, animated: true, completion: nil)

Actions can be added to the view controller that are associated to UIButtons inside it. e.g. alert.addAction(NSLocalizedString("cancel", comment: ""), style: .cancel, actionBlock: nil

An action is a struct defined as so:

struct Action {
 var title: String
 var style: ActionStyle
 var actionBlock: (() -> Void)?
 var dismissAfterAction: Bool
 var hasLoadingIndicator: Bool
 }

The code block in an action is handled in this method:

fileprivate dynamic func doAction(_ sender: CustomButton) {
 // Make sure the action should be allowed for default only.
 let action = actions[sender.tag]
 guard sender.tag >= 0 && sender.tag < actions.count else {
 print("No action at that index.", logType: .Error)
 return
 }
 if let block = action.actionBlock {
 if action.dismissAfterAction {
 dismiss(animated: true, completion: { 
 block()
 })
 } else {
 block() // The point the crash occurs!
 }
 } else {
 dismiss(animated: true, completion: nil)
 }
 }

There is an option when creating an action to dismissAfterAction i.e. auto dismiss the view controller then perform the code block. However if dismissAfterAction is false the app will crash with a malloc error. It doesn't always crash but if I repeatedly tap the button associated with that action it will eventually. Not sure what's going on here. Has anyone come across anything like this before? Seems to be a problem with the code block.

Peter Hornsby
4,2661 gold badge29 silver badges45 bronze badges
asked Nov 5, 2016 at 6:27
1
  • Can you post any of the crash log? I would check out what your actually doing in that closure. Commented Nov 5, 2016 at 13:03

1 Answer 1

1

I've had likely issue, some time ago. As a fix you can change your code in this way:

 //we already checked action block, so 
 action.actionBlock!()
 //block() The point the crash occurs!
answered Nov 5, 2016 at 7:19
Sign up to request clarification or add additional context in comments.

2 Comments

wow that works! why does it work though? why can't I use block from the let statement?
I don't know for now, I should add this to investigations stack.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.