2015-03-03 83 views
0

的AppDelegate:UIAlertView中委託功能不被調用

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

我接收UIAlertView與來自APNS正確的消息時發送一個推送通知。問題是當在UIAlertView中按下兩個按鈕中的任何一個時,記錄不會通過println記錄。

任何想法?

+0

'UpdatesViewController()'創建UpdatesViewController'的'的新實例。這將在代理被調用之前發佈,因爲它不保留在任何地方。你是否試圖在現有的視圖控制器中調用該函數? – rakeshbs 2015-03-03 11:33:01

+1

更好地說「沒有強有力的參考」。 – zaph 2015-03-03 11:40:28

回答

1

截至iOS8 UIAlertView代表已棄用。 改爲使用UIAlertViewController

1

UIAlertView已棄用。改爲使用UIAlertController而使用preferredStyleUIAlertControllerStyleAlert

//示例代碼顯示警報

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