我研究过这个,我很确定这是正确的方法,但我一直在
fatal error: unexpectedly found nil while unwrapping an Optional value
在prepareForSegue行
上 selectSingleVC!.delegate = self
相关的代码位在这里。我不确定为什么nil在那里,它让我发疯: - )
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)
}
})
}
我已经绞尽脑汁待了几个小时,在我的错误中需要一些帮助。感谢
答案 0 :(得分:0)
检查selectSingleVC
。它崩溃是因为selectSingleVC
是nil
,您确定它是SelectSingleViewController
类型,并尝试使用强制解包!
操作。
我的猜测是标识符selectSingleSegue
的segue未与故事板中的SelectSingleViewController
相关联。请仔细检查您的连接。
答案 1 :(得分:0)
segue.destinationViewController
提到的等于UINavigationViewController
,因为Swift 2的变化
let selectSingleVC = segue.destinationViewController.topViewController as? SelectSingleViewController
将不再有效。我修改后的代码是
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "messagesChatSegue" {
let chatVC = (segue.destinationViewController as! UINavigationController).topViewController as! ChatViewController
//chatVC.hidesBottomBarWhenPushed = true
let groupId = sender as! String
chatVC.groupId = groupId
} else if segue.identifier == "selectSingleSegue" {
let selectSingleVC = (segue.destinationViewController as! UINavigationController).topViewController as! SelectSingleViewController
print(segue.destinationViewController)
selectSingleVC.delegate = self
} else if segue.identifier == "selectMultipleSegue" {
let selectMultipleVC = (segue.destinationViewController as! UINavigationController).topViewController as!SelectMultipleViewController
selectMultipleVC.delegate = self
}
}