繁体   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