在Swift中显示UIAlertView时出现错误

12

我正在尝试在我的Swift应用程序中显示UIAlertView。

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

在[_UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:] ()中出现了BAD_ACESS错误

所以我怀疑是self的问题。我将它设为nil。

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

在以下方法中发生了ARM_DA_ALIGN错误: -[_UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:] ()


完整的代码

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

如何正确地做? :)


可能是重复的问题:如何在Swift中创建UIAlertView? - John Riselvato
@JohnRiselvato,它确实没有:http://en.wikipedia.org/wiki/Deprecation - Daij-Djan
我在一个iOS 7应用程序中遇到了类似的问题,在iOS 8上进行了测试。所有的Objective-C代码都会出现访问错误。我已经向苹果Bug报告中心报告了此问题,并被苹果工程师要求提供更多信息。这不是Swift的问题,我认为它的范围更广,与iOS 8 UI框架有关。 - bauerMusic
@downvoter:为什么要点踩? - Daij-Djan
显示剩余2条评论
7个回答

27

Swift 5

你应该这样做:

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)

我会尝试,但是“已弃用”不应该意味着“已损坏”。我有很多使用UIAlertViews的代码...哎。 - Daij-Djan
http://en.wikipedia.org/wiki/Deprecation -- 但我猜现在还没有实施 - Daij-Djan
是的,我绝对不会期望在iOS 8、Swift或其他情况下,这种事实上的UIAlertView构造会抛出EXC_BAD_ACCESS异常。这都发生在我学习Swift的前10分钟内;啊,好吧,系好安全带,让乐趣开始吧! - Chris Hatton
@itedi 很不幸,是的。 "旧" UIAlertView 仍然可用,但会收到弃用警告。请参见约翰的答案。 - dibi
为什么有些人在 OP 明确要求使用 UIAlertView 时却回复了 UIAlertController 的代码呢?而且 UIAlertController 在今天约占 50% 的设备上无法使用 iOS 7,这是为什么?! - Maciej Swic

15

尽管在iOS8中,UIAlertView已被弃用,但你仍然可以使用它,但不要通过它的init函数。例如:

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

至少这是目前我能够成功使用UIAlertView的唯一方法。 但我不确定这样做有多安全。


1
这是我用来在Swift中制作警报弹窗的代码。
let myAlert = UIAlertView(title: "Invalid Login",     
message: "Please enter valid user name",       
delegate: nil, cancelButtonTitle: "Try Again") 
myAlert.show()

0

我成功地使用以下代码显示了一个UIAlertView:

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

代码 "alert.addButtonWithTitle("Understood")" 添加了一个按钮,供用户在阅读完错误消息后点击。
希望这可以帮到您。

0

Xcode 10,Swift 4.2 版本

        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)

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)

处理操作:
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")
    }
}))

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接