gpt4 book ai didi

ios - UIAlertAction 的处理程序不起作用

转载 作者:行者123 更新时间:2023-11-28 06:54:24 26 4
gpt4 key购买 nike

我是 swift 的新手,现在正在学习一本教程书。这本书有点过时了,我遇到了这个错误。

基本上我正在尝试进行表格单元格选择,在我选择单元格后它应该弹出一个菜单然后我可以点击调用按钮。然而,在我点击调用按钮后,预期的更改框没有出现,编译器给我错误:Attempting to load the view of a view controller while is deallocating is not允许并可能导致未定义的行为

当我编辑代码时没有错误,它只是没有正确运行。

另一个问题是我的 table 上有大约 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 中呈现 View Controller - 它应该如下所示:

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

关于ios - UIAlertAction 的处理程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34118043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com