簡體   English   中英

UIAlertAction的處理程序不起作用

[英]UIAlertAction's handler doesn't work

我是新手,現在正在學習一本教程書。 這本書有點過時了,我有這個錯誤。

基本上,我試圖進行表格單元格的選擇,選擇單元格后,它應該會彈出一個菜單,然后可以點擊通話按鈕。 但是,在我按下調用按鈕后,現在沒有出現預期的更改框,並且編譯器給了我錯誤: 不允許嘗試在取消分配時加載視圖控制器的視圖,這可能導致未定義行為

編輯代碼時沒有錯誤,只是無法正常運行。

另一個問題是我的桌子上大約有17行,刺激器的屏幕上僅顯示7行,而我無法向下滾動以查看其余的行。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath){

    //this creates an option menu when you tap on the row
    let optionMenu = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .ActionSheet)
    //this is what happened after you hit the cancel option
    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
    //defines the action after tap on the call option
    let nocallAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    let callActionHandler = {(action:UIAlertAction!)-> Void in
        let alertMessage = UIAlertController(title: "Service Unavaliable", message: "Sorry, the call is not avaliable yet, please retry later.", preferredStyle: .Alert)
        alertMessage.addAction(nocallAction)
    }
    //this is what happened after you hit the call option
    let callAction = UIAlertAction(title: "Call " + "123-000-\(indexPath.row)", style:
        UIAlertActionStyle.Default, handler: callActionHandler)
    //this is what happened after you hit the been there option
    let isVisitedAction = UIAlertAction(title: "I've been here", style: .Default, handler: {
        (action:UIAlertAction!) -> Void in
        let cell = tableView.cellForRowAtIndexPath(indexPath)
        cell?.accessoryType = .Checkmark
        })
    //add the action to the option menu
    optionMenu.addAction(isVisitedAction)
    optionMenu.addAction(callAction)
    optionMenu.addAction(cancelAction)

    self.presentViewController(optionMenu, animated: true, completion: nil)

}

您永遠不會在callActionHandler中顯示視圖控制器-它應如下所示:

let callActionHandler = {(action:UIAlertAction!)-> Void in
    let alertMessage = UIAlertController(title: "Service Unavaliable", message: "Sorry, the call is not avaliable yet, please retry later.", preferredStyle: .Alert)
    alertMessage.addAction(nocallAction)

    self.presentViewController(alertMessage, animated: true, completion: nil)
}

Swift還允許您與UIAlertAction同時創建完成處理程序,我認為它更具可讀性:

    let callAction = UIAlertAction(title: "Call " + "123-000-243534", style: UIAlertActionStyle.Default) 
    { action in
        let alertMessage = UIAlertController(title: "Service Unavaliable", message: "Sorry, the call is not avaliable yet, please retry later.", preferredStyle: .Alert)
        alertMessage.addAction(nocallAction)

        self.presentViewController(alertMessage, animated: true, completion: nil)
    }

暫無
暫無

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

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