ios - 参数作为按钮选择器 - iOS (Swift)

标签 ios swift selector

当网络连接缓慢或断开连接时,我尝试执行内部应用程序通知。

如果用户想要重新加载,我想调用传入参数(回调)的函数,但 Xcode 不允许我这样做:

Argument of '#selector' cannot refer to parameter 'callback'

这是我的代码:

func listenForFirebaseConnection(callback: @escaping () -> Void) {
    FireDataUtil().getBase().child(".info/connected").observeSingleEvent(of: .value, with: { (snapshot) in
            if snapshot.exists() {
                let isConnected = snapshot.value as! Bool?
                if isConnected != nil && isConnected == false {

                    let view = MessageView.viewFromNib(layout: .MessageView)
                    view.button?.isHidden = false

                    // Theme message elements with the warning style.
                    view.configureTheme(.error)

                    // Add a drop shadow.
                    view.configureDropShadow()

                    // Set message title, body, and icon. Here, we're overriding the default warning
                    // image with an emoji character.
                    view.configureContent(title: "Network Issue", body: "It looks like your network connection is either slow or disconnected")
                    view.button?.setTitle("Reload", for: .normal)

                    view.button?.addTarget(self, action: #selector(callback), for: .touchUpInside)
                    var config = SwiftMessages.Config()

                    config.presentationContext = .window(windowLevel: UIWindowLevelStatusBar)
                    config.presentationStyle = .bottom
                    config.duration = .seconds(seconds: 5)

                    // Show the message.
                    SwiftMessages.show(config: config, view: view)
                }
            }
        })
}

最佳答案

尝试这样:

var objCallback:() -> Void)?

func listenForFirebaseConnection(callback: @escaping () -> Void) {
FireDataUtil().getBase().child(".info/connected").observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.exists() {
            let isConnected = snapshot.value as! Bool?
            if isConnected != nil && isConnected == false {

                self.objCallback = callback
                let view = MessageView.viewFromNib(layout: .MessageView)
                view.button?.isHidden = false

                // Theme message elements with the warning style.
                view.configureTheme(.error)

                // Add a drop shadow.
                view.configureDropShadow()

                // Set message title, body, and icon. Here, we're overriding the default warning
                // image with an emoji character.
                view.configureContent(title: "Network Issue", body: "It looks like your network connection is either slow or disconnected")
                view.button?.setTitle("Reload", for: .normal)

                view.button?.addTarget(self, action: #selector(objCallback), for: .touchUpInside)
                var config = SwiftMessages.Config()

                config.presentationContext = .window(windowLevel: UIWindowLevelStatusBar)
                config.presentationStyle = .bottom
                config.duration = .seconds(seconds: 5)

                // Show the message.
                SwiftMessages.show(config: config, view: view)
            }
        }
    })

}

这可能对你有帮助

关于ios - 参数作为按钮选择器 - iOS (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42389010/

相关文章:

ios - Parse PushError 141 invalid function 云代码

ios - Swift - UIViews 以编程方式排序

ios - 尝试创建类的实例时使用未解析的标识符错误

jquery - 如何使用 jQuery 选择非直接左兄弟 <span> ?

ios - iOS 中的 View 管理

ios - 在 UICollectionView 中禁用滚动并调整其大小以包裹所有单元格

oop - 为什么协议(protocol)的关联类型在 Swift 中不使用泛型语法?

ios - 如何正确测试游戏中心成就

Jquery $ ("img").click(函数()选择器不工作

selector - 在 Swift 中使用选择器有类型安全的方法吗?