我是swift的新手,现在正在学习一本教程。这本书有点过时了,我有这个错误。
基本上我尝试进行表格单元格选择,在我选择单元格之后应该弹出一个菜单然后我可以点击通话按钮。但是,现在我点击调用按钮后,预期的更改框不会显示,编译器会给我错误: 尝试在取消分配时加载视图控制器的视图是不允许的,可能导致未定义的行为
编辑代码时没有错误,它只是无法正常运行。
另一个问题是桌子上有大约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)
}
答案 0 :(得分:1)
您永远不会在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)
}