xcode Swift 中的 UIAlertView,获取 EXC_BAD_ACCESS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24029768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIAlertView in Swift, getting EXC_BAD_ACCESS
提问by Brandon Buck
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.
首先,我很清楚 Xcode 6 和 Swift 语言处于 Beta 阶段,容易出错;然而,这个特殊的似乎有些奇怪,因为到目前为止我尝试过的所有其他方法似乎都可以正常工作。
If this is not appropriate for StackOverflow, I will gladly remove the question.
如果这不适合 StackOverflow,我很乐意删除这个问题。
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:
我已经开始使用 Xcode 6/Swift(准备发布),与我想象的相比,这是一次非常愉快的体验。话虽如此,在移植我喜欢做的“培训”风格的应用程序时的一个问题是,由于EXC_BAD_ACCESS
有问题的代码,我似乎无法生成 UIAlertView :
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.
在创建 UIAlertView 的行上,我得到一个EXC_BAD_ACCESS
因为[UIAlertView retain]
在释放的实例上被调用。
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.
再一次,我把这归结为测试版横幅,但很好奇我是否做错了什么,或者其他人是否遇到了类似的问题。
回答by iPatel
Try the following code
试试下面的代码
let alert = UIAlertView()
alert.title = "Title"
alert.message = "My message"
alert.addButtonWithTitle("Ok")
alert.show()
But in iOS 8
但在 iOS 8
UIAlertView
is deprecated. So use UIAlertController
with a preferredStyle
of UIAlertControllerStyleAlert
. It should be:
UIAlertView
已弃用。因此,使用UIAlertController
了preferredStyle
的UIAlertControllerStyleAlert
。它应该是:
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 ?
检查上面的代码,您是否遇到相同的错误?
回答by Brandon Buck
From Xcode 6.0 UIAlertView class:
从 Xcode 6.0 UIAlertView 类:
UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.
UIAlertView 已弃用。使用 UIAlertController 和首选样式 UIAlertControllerStyleAlert 代替。
On swift ( iOS 8 and OS X 10.10 ), you can do this:
在 swift ( iOS 8 和 OS X 10.10 ) 上,你可以这样做:
var alert = UIAlertController(
title: "Send",
message: "You have successfully send your feedback.",
preferredStyle: UIAlertControllerStyle.Alert
)
alert.addAction(UIAlertAction(
title: "Ok",
style: UIAlertActionStyle.Default,
handler: nil
))
self.presentViewController(alert, animated: true, completion: nil)
回答by Divyang Shah
2 things u have to use delegate:self cancelButtonTitle ends with nil
你必须使用委托的 2 件事:self cancelButtonTitle 以 nil 结束