Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing an alert view in Swift causes EXC BAD ACCESS [duplicate]

Tags:

ios

swift

I have the following view controller. It simply reads from a text field a value to display in a UIAlertView.

import UIKit

class ViewController: UIViewController {

    @IBOutlet var textField: UITextField!

    @IBAction func pressButton(sender: UIButton) {
        let name = textField.text

        let alert = UIAlertView(
            title: "Hello!",
            message: "How are you today, \(name). I'm lovely!",
            delegate: nil,
            cancelButtonTitle: "Thanks!"
        )

        alert.show() // EXC_BAD_ACCESS
    }
}

Why does alert.show() crash with EXC_BAD_ACCESS? What's happening here to my instance of UIAlertView? Why isn't it in alert like I think it should be?

like image 662
Alex Wayne Avatar asked Sep 15 '25 10:09

Alex Wayne


1 Answers

Try to use var instead of lat as following

var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
like image 154
Nirav Gadhiya Avatar answered Sep 16 '25 22:09

Nirav Gadhiya