Daij-Djan
Daij-Djan

Reputation: 50099

Error showing a UIAlertView in swift

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? :)

Upvotes: 12

Views: 19596

Answers (7)

dibi
dibi

Reputation: 3275

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)

Upvotes: 27

Mirac Bektas
Mirac Bektas

Reputation: 115

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)

Upvotes: 0

swift2geek
swift2geek

Reputation: 1812

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)

Upvotes: 0

Bigfoot11
Bigfoot11

Reputation: 921

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.

Upvotes: 0

Harsh Deep Singhal
Harsh Deep Singhal

Reputation: 1

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")
    }
}))

Upvotes: 0

BlackLamb
BlackLamb

Reputation: 21

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()

Upvotes: 1

John Riselvato
John Riselvato

Reputation: 12904

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.

Upvotes: 15

Related Questions