簡體   English   中英

Swift Alert彈出窗口出現在模擬器iphone4s(8.1)中,而不是iphone4s(7.1)中

[英]Swift Alert popup is appearing in simulator iphone4s(8.1), not iphone4s (7.1)

我編寫了代碼,以在單擊按鈕時顯示一個簡單的警報彈出窗口。 在模擬器iPhone 4s(8.1)中嘗試該應用程序時,它可以按預期運行,但是在模擬器iPhone 4s(7.1)中嘗試時,該應用程序始終崩潰。

這里的代碼:

@IBAction func buttonPressed(sender:UIButton){

    let controller = UIAlertController(title: "This is a title", message: "Hello, my friend", preferredStyle: UIAlertControllerStyle.Alert)
    let cancelAction = UIAlertAction(title: "Phew!", style: .Cancel, handler: nil)
    controller.addAction(cancelAction)
    self.presentViewController(controller, animated: true, completion: nil)
}

這是錯誤消息:

第一行代碼(創建“ controller”常量的代碼)以綠色突出顯示,並顯示消息“線程1:EXC_BAD_ACCESS(Code = 1,address = 0x10)”。

我將不勝感激任何幫助

UIAlertController適用於iOS> = 8.0

您必須為iOS <8.0使用UIAlertView

不幸的是,UIAlertController僅在iOS 8.0和更高版本中可用,這里是文檔,並且在右側進行了說明: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/#//apple_ref/ DOC / UID / TP40014538-CH1-SW2

我相信這取代了現在不推薦使用的UIAlertView,可以在Apple文檔中找到: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html#//apple_ref/occ/cl / UIAlertView中

Swift不喜歡舊的,而是考慮和“ if”子句,並使用該應用為相應的iOS系統創建UIAlertView和UIAlertController。

感謝Dom Bryan推薦的鏈接,我管理了以下解決方案:

@IBAction func buttonPressed(sender:UIButton){

    if NSClassFromString("UIAlertController") == nil{
        let alert = UIAlertView(title: "This is a title", message: "I am an iOS7 alert", delegate: self, cancelButtonTitle: "Phew!")
        alert.show()
    }else{
        let controller = UIAlertController(title: "This is a title", message: "Hello, my friend", preferredStyle: .Alert)
        let cancelAction = UIAlertAction(title: "Phew!", style: .Cancel, handler: nil)
        controller.addAction(cancelAction)
        self.presentViewController(controller, animated: true, completion: nil)
    }
}

而且在iOS7和iOS8設備上運行正常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM