0

So I have a map search function that has a text field. I set up an UIAlertController there, and it works fine, i.e. it brings up an alert when there's not location nearby.

I have a current location button, but I want to perform action only if the user allows for current location status to be turned on. Otherwise, I want to display an alert. This is where my error comes in. The frustrating part is that I have a print statement when access is not granted, and it displays that, and skips the alert entirely. I tried removing the statements and just the alert come up without any other code in the button... Still nothing. What's going on?

@IBAction func currentLocationBtn(sender: UIBarButtonItem) {

        checkLocationAuthorizationStatus()

        if self.locationAuthorized {
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            self.currentLocation = CLLocation(latitude: locationManager.location.coordinate.latitude, longitude: locationManager.location.coordinate.longitude)
        } else {
            println("Current Location is disabled")

            let alertController = UIAlertController(title: "Current Location Turned Off", message:
                "Sorry, you need to turn on Current Location to use this feature", preferredStyle: UIAlertControllerStyle.Alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
        }


    }

1 Answers1

1

You're setting up the alert controller and adding the Dismiss action - but you're not actually presenting anything. Just like any other UIViewController, you need to present a UIAlertController in order for it to be displayed.

Assuming you're working within a view controller, one way of doing this would be to call the following right after you add the action:

self.presentViewController(alertController, animated: true, completion: nil)
Rich R
  • 149
  • 1
  • 9
  • Thanks! This worked. I'm so confused though. I actually have code in a text field, that presents another alertcontroller. And for some reason, I don't need to present it there... Any idea why? – Yan Ping Huang Jul 19 '15 at 07:19
  • Glad to hear it worked! If you could mark my answer as accepted that'd be great. As for the other alert controller - I'd need to see the code in question, but they will generally always need to be presented in order to be displayed. – Rich R Jul 19 '15 at 07:24