未调用UIAlertView委托函数

时间:2015-03-03 11:07:54

标签: ios objective-c swift uialertview

AppDelegate:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: NSDictionary) {
    let aps: NSDictionary = userInfo.objectForKey("aps") as NSDictionary
    UpdatesViewController().newArticle(aps) 
}

UpdatesViewController:

class UpdatesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate {
    func newArticle(userInfo: NSDictionary) {
        let title: NSString = userInfo["alert"] as NSString
        UIAlertView(title: "New update", message: title, delegate: self, cancelButtonTitle: "Dismiss", otherButtonTitles: "Read more").show()
    }

    func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) {
        println(buttonIndex)
    }

    func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
        println(buttonIndex)
    }
}

发送推送通知时,我收到来自APNS的正确消息的UIAlertView。问题是,当在UIAlertView中按下两个按钮中的任何一个按钮时,不会通过println记录按下。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

从iOS8 UIAlertView代表不推荐使用。 请改用UIAlertViewController

答案 1 :(得分:1)

UIAlertView已弃用。请改为使用UIAlertController preferredStyle代替UIAlertControllerStyleAlert

//示例代码显示警告

let alertController = UIAlertController(title: "Default Style", message: "A standard alert.", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { enter code here }