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

时间:2017-02-22 10:45:55

标签: ios swift selector

当网络连接速度很慢或断开连接时,我正在尝试进行内部应用通知。

如果用户想重新加载我想调用参数(回调)中传递的函数,但Xcode不会让我这样做:

  

'#selector'的参数不能引用参数'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)
                }
            }
        })
}

1 个答案:

答案 0 :(得分:0)

尝试这样:

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)
            }
        }
    })

}

这可能有助于你