ios - 在#selector 函数中传递数据

标签 ios swift uibutton mapkit

我无法确定点击按钮时传递数据的最佳方式。我还想在点击按钮时为按钮设置动画。该按钮位于 map 注释的自定义标注 View 内。我尝试设置一个类属性 dogIDOfUpvoteTapped,并使用它来存储数据,以便被调用的函数可以访问它。不幸的是,当我想与按钮本身交互时,事情变得复杂了。

如何从调用的函数“upvoteButtonTapped”中获取对“CustomAnnotation”对象的引用?我省略了一堆不相关的代码,但这是我正在使用的代码:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if view.annotation is MKUserLocation {
        return
    }

    let customAnnotation = view.annotation as! CustomAnnotation
    let views = Bundle.main.loadNibNamed("CustomCalloutView", owner: nil, options: nil)
    let calloutView = views?[0] as! CustomCalloutView

    dogIDOfUpvoteTapped = customAnnotation.dogID

    calloutView.scoreLabel.text = text
    calloutView.creatorLabel.text = customAnnotation.creator
    calloutView.dogImageView.image = customAnnotation.picture
    calloutView.upvoteButton.addTarget(self, action: #selector(upvoteTapped), for: .touchUpInside)

    view.addSubview(calloutView)
}

这是 upvoteTapped 的函数,尽管我在那里所做的一切都与问题无关。

@objc func upvoteTapped() {
    let ref = Database.database().reference().child("dogs").child(dogIDOfUpvoteTapped)
    ref.child("upvotes").observeSingleEvent(of: .value, with: { (snap) in
        var currentUpvotes = Int(snap.value as! String)!
        currentUpvotes += 1
        ref.child("upvotes").setValue(String(currentUpvotes))
    })

}

最佳答案

@leo-dabus 说得对,您需要使用带有发件人(按钮)的方法变体。

此外,在该按钮上附加一个标签以在集合中标识它,注意它必须是整数,并且我假设您正在使用或有权访问数据库项目列表中的整数 ID 或索引 (dogID) .

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if view.annotation is MKUserLocation {
        return
    }

    let customAnnotation = view.annotation as! CustomAnnotation
    let views = Bundle.main.loadNibNamed("CustomCalloutView", owner: nil, options: nil)
    let calloutView = views?[0] as! CustomCalloutView

    dogIDOfUpvoteTapped = customAnnotation.dogID

    calloutView.scoreLabel.text = text
    calloutView.creatorLabel.text = customAnnotation.creator
    calloutView.dogImageView.image = customAnnotation.picture
    calloutView.upvoteButton.tag = dogIDOfUpvoteTapped
    calloutView.upvoteButton.addTarget(self, action: #selector(upvoteTapped:), for: .touchUpInside)

    view.addSubview(calloutView)
}

@objc func upvoteTapped(_ button: UIButton) {
    let ref = Database.database().reference().child("dogs").child(button.tag)
    ref.child("upvotes").observeSingleEvent(of: .value, with: { (snap) in
        var currentUpvotes = Int(snap.value as! String)!
        currentUpvotes += 1
        ref.child("upvotes").setValue(String(currentUpvotes))
    })

}

没有看到 customAnnotation.dogID 的类,我假设它是一个整数,如果它是一个字符串或其他,您将需要使用一个主键或索引是一个 int。可以公平地猜测主键是一个 int。

关于ios - 在#selector 函数中传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47192485/

相关文章:

ios - 约束在 UITableViewCell 中无法正常工作。如何正确设置?

objective-c - 按下按钮时出现无法识别的选择器错误

ios - 在新的 Swift View Controller 文件上添加按钮不起作用

ios - 我的功能有什么问题

ios - 在我设置 callEventHandler 后,CTCallCenter 不更新 currentCalls 属性

swift - 如何在 Swift 中获取多行字符串文字的索引

iOS Swift - Google Place API(Web)以奇怪的格式返回 JSON

ios - 寻找 iOS 风格的快进和快退按钮图像

ios - 在 didRangeBeacons 方法中触发推送通知

ios - 生成歌曲建议(自动完成)