Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

ios - UIAlertView in Swift, getting EXC_BAD_ACCESS

First and foremost, I'm quite aware that Xcode 6 and the Swift language are in Beta and are prone to errors; however, this particular one seems to be something strange as everything else I've tried so far seems to work fine.

If this is not appropriate for StackOverflow, I will gladly remove the question.

I've begin playing with Xcode 6/Swift (preparing for its release) and it has been an extraordinarily enjoyable experience compared to what I thought it would be. That being said, one issue in porting a "training" style app I like to do is that I can't seem to generated a UIAlertView due to EXC_BAD_ACCESS the code in question is:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    var alert = UIAlertView(title: "Title", message: "Message", delegate: nil, cancelButtonTitle: "OK") // EXC_BAD_ACCESS here
    alert.show()
}

On the line that creates the UIAlertView I get an EXC_BAD_ACCESS because [UIAlertView retain] was called on a deallocated instance.

Again, I'm chalking this up to the beta banner but was curious if I was doing something wrong or if anyone else has run into similar issues.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try the following code

let alert = UIAlertView()
alert.title = "Title"
alert.message = "My message"
alert.addButtonWithTitle("Ok")
alert.show()

But in iOS 8

UIAlertView is deprecated. So use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert. It should be:

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)

Check the above code, are you getting same error or not ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...