12

Im trying to show a UIAlertView in my swift App

    alert = UIAlertView(title: "",
        message: "bla",
        delegate: self,
        cancelButtonTitle: "OK")
    alert!.show()

=> BAD_ACESS error in: -[_UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:] ()

so I suspected self. I set it to nil

    alert = UIAlertView(title: "",
        message: "bla",
        delegate: nil,
        cancelButtonTitle: "OK")
    alert!.show()

=> ARM_DA_ALIGN error in: -[_UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:] ()


the full code

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UIAlertViewDelegate {

    @lazy var window = UIWindow(frame: UIScreen.mainScreen().bounds)
    @lazy var locationManager = CLLocationManager()
    var alert: UIAlertView? = nil

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        //setup dummy window
        self.window.backgroundColor = UIColor.whiteColor()
        self.window.rootViewController = UIViewController()
        self.window.makeKeyAndVisible()

        alert = UIAlertView(title: "",
            message: "bla",
            delegate: nil,
            cancelButtonTitle: "OK")
        alert!.show()

    return true
    }
}

How to do it right? :)

Popeye
  • 11,839
  • 9
  • 58
  • 91
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • possible duplicate of [How would I create a UIAlertView in Swift?](http://stackoverflow.com/questions/24022479/how-would-i-create-a-uialertview-in-swift) – John Riselvato Jun 04 '14 at 14:52
  • it does go to _UIAlertViewAlertControllerShim so I feel this is a bug -- I mean. deprecation shouldn't mean no longer available :) – Daij-Djan Jun 04 '14 at 14:53
  • @JohnRiselvato it surely doesn't: http://en.wikipedia.org/wiki/Deprecation – Daij-Djan Jun 04 '14 at 14:58
  • @JohnRiselvato I don't want stuff if(ios8) UIAlertViewController else UIAlertView :) – Daij-Djan Jun 04 '14 at 15:04
  • 1
    possible duplicate of [UIAlertView in Swift, getting EXC\_BAD\_ACCESS](http://stackoverflow.com/questions/24029768/uialertview-in-swift-getting-exc-bad-access) – iPatel Jun 05 '14 at 11:36
  • I have a similar problem with an iOS 7 app, tested in iOS 8. **All Objective-C code**. Fails with bad access. I have reported in Apple Bug Report and was asked for more information by Apple Engineering. **This is not a swift issue**, I believe its scope is bigger and has to do with iOS 8 UI Framework. – bauerMusic Jul 13 '14 at 06:24

7 Answers7

27

Swift 5

You should do it this way:

let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Button", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
Gilad Brunfman
  • 3,452
  • 1
  • 29
  • 29
dibi
  • 3,257
  • 4
  • 24
  • 31
  • I will try that but deprecated shouldn't mean broken. I have tons of code that uses UIAlertViews... oh well – Daij-Djan Jun 04 '14 at 14:52
  • http://en.wikipedia.org/wiki/Deprecation -- but I guess it isn't implemented for now – Daij-Djan Jun 04 '14 at 14:59
  • Yeah I would definitely not expect this de-facto construction of a UIAlertView to throw EXC_BAD_ACCESS in iOS 8, Swift or otherwise. All this in my first 10 minutes of Swift; ah well, buckle up and let the fun begin! – Chris Hatton Jun 07 '14 at 13:49
  • @itedi Unfortunately yes. The "old" UIAlertView is still available but you'll get a deprecated warning. See John's answer. – dibi Jul 15 '14 at 20:07
  • Why do people respond with UIAlertController code when the OP clearly asks for UIAlertView and UIAlertController doesn't work on iOS 7 which is on about 50% devices today?! – Maciej Swic Nov 14 '14 at 17:38
15

Even though UIAlertView is depreciated in iOS8, you can get away with using it but not through it's init function. For example:

    var alert = UIAlertView()
    alert.title = "Title"
    alert.message = "message"
    alert.show()

Atleast this is the only way so far I've been able to successfully use an UIAlertView. I'm unsure on how safe this is though.

John Riselvato
  • 12,854
  • 5
  • 62
  • 89
1

This is what I have used to make an alert popup in swift.

let myAlert = UIAlertView(title: "Invalid Login",     
message: "Please enter valid user name",       
delegate: nil, cancelButtonTitle: "Try Again") 
myAlert.show()
BlackLamb
  • 21
  • 5
0
var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)

To handle actions:

alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { 
    action in switch action.style {
    case .Default:
        println("default")
    case .Cancel:
        println("cancel")
    case .Destructive:
        println("destructive")
    }
}))
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
0

I was successfully able to show a UIAlertView using this code:

var alert = UIAlertView()
alert.title = "Title"
alert.message = "Message"
alert.addButtonWithTitle("Understood")
alert.show()

The code "alert.addButtonWithTitle("Understood")" adds a button for the user to push after they have read the error message.

Hope this helps you.

Bigfoot11
  • 911
  • 2
  • 11
  • 25
0

Xcode 10, Swift 4.2 version

        let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction(title: "Button", style: UIAlertAction.Style.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
Community
  • 1
  • 1
swift2geek
  • 1,697
  • 1
  • 20
  • 27
0
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertController.Style.alert)
                    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
                    self.present(alert, animated: true, completion: nil)
vimuth
  • 5,064
  • 33
  • 79
  • 116
Mirac Bektas
  • 115
  • 1
  • 2