• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:15:01 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我无法确定点击按钮时传递数据的最佳方式。我还想在点击按钮时为其设置动画。该按钮位于 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))
    })

}



Best Answer-推荐答案


@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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap