我正在尝试按照以下代码执行条件segue,如果userid仅与当前用户id匹配,则将执行segue,但未按要求进行,它将不执行条件语句而执行segue,调用func toChatView的条件必须为真
该按钮位于表View Controller中的表单元格中,必须从中调用该动作
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! CommentCell
cell.itemLikebutton.tag = indexPath.row
cell.itemLikebutton.addTarget(self, action: #selector(likeaction1(_:)), for: .touchUpInside)
cell.chatButton.tag = indexPath.row
cell.chatButton.addTarget(self, action: #selector(toChatView(_:)), for: .touchUpInside)
cell.set(comment: comments[indexPath.row])
return cell
}
@IBAction func toChatView(_ sender: Any) {
var currentUserId = Auth.auth().currentUser!.uid
if currentUserId == userId1 || currentUserId == UserId2{
performSegue(withIdentifier: "toMessageRoom", sender: self)
}
else{
self.view.makeToast("You are not authorized for this chatroom")
}
}
现在我在准备函数中遇到此错误
else if (segue.identifier == "toMessageRoom"){
let chatbutton = sender as! UIButton
let comment = comments[chatbutton.tag]
var commentKey1 = comment._documentId
var commentUserID = comment._commentAuthorId
var messageRoomId = postId + postAuthorId + commentKey1! + commentUserID!
var vc = segue.destination as! ChatViewController
vc.MessageRoomPostId = postId
vc.MessageRoomCommentId = commentKey1!
vc.messageRoomIdFinal = messageRoomId
print(postId + "Check Message Configuration" + commentKey1!)
}
错误是
无法将类型'BlogApp2.CommentListViewController'(0x10b31d280)的值强制转换为'UIButton']
直接将segues链接到按钮(等)很容易快速触发segue,而无需调用performSegue
的其他代码,但是如果需要有条件地触发segue,则不能使用它。
确保您的IBAction
方法也链接到相应的按钮。根据您单元格中的内容,您可能希望使用UITableViewDelegate
方法tableView(_:didSelectRowAt:)
代替IBAction。
performSegue(withIdentifier: "toMessageRoom", sender: self)
to
performSegue(withIdentifier: "toMessageRoom", sender: sender)