香港专业教育学院研究这一点,我很确定这是正确的方法,但我不断
fatal error: unexpectedly found nil while unwrapping an Optional value
在prepareForSegue行上
selectSingleVC!.delegate = self
相关的代码位在这里。我不知道为什么没有零零零零碎碎的东西让我发疯:-)
class FriendsChatViewController: UITableViewController, UIActionSheetDelegate, SelectSingleViewControllerDelegate, SelectMultipleViewControllerDelegate {
...
@IBAction func compose(sender: UIBarButtonItem) {
weak var wself: FriendsChatViewController? = self
let action = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { _ in
}
let singleAction = UIAlertAction(title: "Single recipient", style: UIAlertActionStyle.Default) { _ in
if let _ = wself {
self.performSegueWithIdentifier("selectSingleSegue", sender: self)
}
}
let multipleAction = UIAlertAction(title: "Multiple recipients", style: UIAlertActionStyle.Default) { _ in
if let _ = wself {
self.performSegueWithIdentifier("selectMultipleSegue", sender: self)
}
}
action.addAction(cancelAction)
action.addAction(singleAction)
action.addAction(multipleAction)
self.presentViewController(action, animated: true, completion: nil)
}
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "messagesChatSegue" {
let chatVC = segue.destinationViewController as! ChatViewController
//chatVC.hidesBottomBarWhenPushed = true
let groupId = sender as! String
chatVC.groupId = groupId
} else if segue.identifier == "selectSingleSegue" {
let selectSingleVC = segue.destinationViewController as? SelectSingleViewController
print(self)
selectSingleVC!.delegate = self <-- ERROR is here
} else if segue.identifier == "selectMultipleSegue" {
let selectMultipleVC = segue.destinationViewController as? SelectMultipleViewController
selectMultipleVC!.delegate = self
}
}
这是我的SelectSingleViewController的相关部分
protocol SelectSingleViewControllerDelegate {
func didSelectSingleUser(user: PFUser)
}
class SelectSingleViewController: UITableViewController, UISearchBarDelegate {
var friends = [PFObject]()
var user = PFUser()
var delegate: SelectSingleViewControllerDelegate!
...
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
self.dismissViewControllerAnimated(true, completion: { () -> Void in
if self.delegate != nil {
let friendObject = self.friends[indexPath.row]
let friend = friendObject["user"] as! PFUser
self.user = friend
print(self.user)
self.delegate.didSelectSingleUser(self.user)
}
})
}
我已经花了好几个小时思考这个问题,需要一些协助来解决我的错误。谢谢
最佳答案
选中selectSingleVC
。它崩溃是因为selectSingleVC
是nil
,并且您确定它的类型是SelectSingleViewController
,并尝试使用强制展开!
操作。
我的猜测是标识符为selectSingleSegue
的segue在故事板上未链接到SelectSingleViewController
。请仔细检查您的连接。