1
votes

I have a Parse Sign Up and I have an UIAlertController and I want the UIAlertController to show up with an indicator but get dismissed with another UIAlertController when there is an error in the sign up.

I have several cases for a failed sign up, all work prior to adding the UIAlertController with the indicator. And the indicator alert controller works fine when the sign up is successful and it get dismissed correctly.

//create an alert controller
    let pending = UIAlertController(title: "Creating New User", message: nil, preferredStyle: .Alert)

    //create an activity indicator
    let indicator = UIActivityIndicatorView(frame: pending.view.bounds)
    indicator.autoresizingMask = .FlexibleWidth | .FlexibleHeight
    //add the activity indicator as a subview of the alert controller's view
    pending.view.addSubview(indicator)
    indicator.userInteractionEnabled = false // required otherwise if there buttons in the UIAlertController you will not be able to press them
    indicator.startAnimating()
    self.presentViewController(pending, animated: true, completion: nil)

Here is the code for the sign up, this works

        if signUpError == nil {
            println("Sign Up Successful")
            // Keep track of the installs of our app
            var installation: PFInstallation =  PFInstallation.currentInstallation()
            installation.addUniqueObject("Reload", forKey: "channels")
            installation["user"] = PFUser.currentUser()
            installation.saveInBackground()


            // to stop the uialertviewcontroller once sign up successful
            pending.dismissViewControllerAnimated(true, completion: {
                self.performSegueWithIdentifier("goToAppFromSignUp", sender: self)

            })

         }

Here is where I am stuck, this is just one of the cases if I can get this to work I can do it for the others.

        else {
            println("Error Sign Up")


            //If email has been used for another account kPFErrorUserEmailTaken
            if(signUpError!.code == 203) {

                let alertController = UIAlertController(title: "Sign Up Failed", message: "Sorry! Email has been taken! ", preferredStyle: .Alert)


                let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
                    // ...
                }
                alertController.addAction(OKAction)



                    self.presentViewController(alertController, animated: true) {
                        // ...
                    }  
            }

My thought was to dismiss the UIAlertController and show the next one in the completion block

pending.dismissViewControllerAnimated(true, completion: {
self.presentViewController(alertController, animated: true) {
                // ...
            }

        })

But the app freezes on the pending alert controller (the one with the indicator). which is already presenting (null)

Any ideas? Thanks.

2
In 'else' part, you haven't dismiss the pending controller.Sohil R. Memon

2 Answers

3
votes

Try to dismiss the pending alert and present the next alert without animation.

pending.dismissViewControllerAnimated(false, completion: nil)
self.presentViewController(alertController, animated: false, completion: nil)
1
votes

In the apple documentation, it states that you should not modify the view hierarchy for UIAlertController. This could cause you problems later and may be part of your current issue.

It is also likely that presenting one while the other is running is causing your issue. Dismissing the first and once complete presenting the second should in theory fix your issue. The fact it hangs suggests something else is happening. You should stop and remove the activity indicator from the view before dismissing it.

A better approach I think would be to consider using a HUD like https://github.com/jdg/MBProgressHUD to display progress or activity rather than trying to use an alert controller for a purpose it was not intended for.

A HUD is great for presenting heads up progress or activity while waiting for things to happen. It has a number of display options including activity with headline text. I would present the HUD when you are waiting and then use the alert controller to present alerts.

I have found using a HUD looks better and is much easier to control.