Swift3 未在Swift中调用代理
我不明白为什么我的代理没有被调用。 下面是我定义协议并调用委托的地方:Swift3 未在Swift中调用代理,swift3,delegation,Swift3,Delegation,我不明白为什么我的代理没有被调用。 下面是我定义协议并调用委托的地方: protocol CommentRatingViewControllerDelegate: class { func didCommentOrRatePost(updatedRating: Bool, addedComment:Bool) } class CommentRatingViewController: UIViewController, UITextViewDelegate { weak var d
protocol CommentRatingViewControllerDelegate: class {
func didCommentOrRatePost(updatedRating: Bool, addedComment:Bool)
}
class CommentRatingViewController: UIViewController, UITextViewDelegate {
weak var delegate:CommentRatingViewControllerDelegate?
...
@IBAction func saveRatingComment(_ sender: Any) {
var updatedRating = false
var addedComment = false
rating = ratingView.rating
if rating != 0.0 {
saveRating(articleID: post.articleID, userID: post.userID)
updatedRating = true
}
if commentsTextView.text != "" {
saveComment(articleID: post.articleID, userID: post.userID, comment: commentsTextView.text!)
addedComment = true
}
self.delegate?.didCommentOrRatePost(updatedRating: updatedRating, addedComment: addedComment)
close()
}
。。。。
这里是符合委托协议的地方:
extension PostDetailViewController: CommentRatingViewControllerDelegate {
func didCommentOrRatePost(updatedRating: Bool, addedComment: Bool) {
if updatedRating == true || addedComment == true {
networkingState = .searching
if updatedRating {
getRating(articleID: post.articleID)
}
if addedComment {
post.numberOfComments += post.numberOfComments
}
networkingState = .finishedSearching
tableView.reloadData()
}
}
}
遵守协议时,如果要调用委托方法,仅使类遵守协议是不够的,还需要在类内将委托设置为
self
class CommentRatingViewController: UIViewController, UITextViewDelegate {
override func viewDidLoad() {
self.delegate = self
}
}
遵守协议时,如果要调用委托方法,仅使类遵守协议是不够的,还需要在类内将委托设置为
self
class CommentRatingViewController: UIViewController, UITextViewDelegate {
override func viewDidLoad() {
self.delegate = self
}
}
在调用委托时设置断点,是否为零?(self.delegate)我在这一行后面加了一个断点:self.delegate?.didCommentOrRatePost(updatedRating:updatedRating,addedComment:addedComment)此时为零。在调用self.delegate之前加一个断点,检查它是否为零,如果为零,则没有为弱引用(self.delegate)分配强引用。另外,在为委托分配REF时向我们显示代码。在哪里设置委托?是,它是零。恐怕我不明白你的意思。当你调用委托的时候,把断点放在零上,是吗?(self.delegate)我在这一行后面加了一个断点:self.delegate?.didCommentOrRatePost(updatedRating:updatedRating,addedComment:addedComment)此时为零。在调用self.delegate之前加一个断点,检查它是否为零,如果为零,则没有为弱引用(self.delegate)分配强引用。另外,在为委托分配REF时向我们显示代码。在哪里设置委托?是,它是零。恐怕我不明白你的意思?是的。。我现在终于明白了。谢谢别担心。如果你觉得我的答案有用,请考虑接受。对不起…我想是的!对我现在终于明白了。谢谢别担心。如果你觉得我的答案有用,请考虑接受。对不起…我想是的!