2017-02-22 51 views
0

我试图做一个内部应用程序通知,当网络连接速度慢或断开。作为按钮选择器的参数 - iOS(Swift)

如果用户想重新加载我想调用参数(回调)通过了功能,但Xcode中不会让我这样做:

参数“#selector”不能引用参数的“回调“

这里是我的代码:

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

你不能调用块的方法或关闭这个样子。要么按照您尝试的方式声明块或闭包的属性来调用。 –

+0

我只是试图做这样的事情:http://stackoverflow.com/a/36983811/6303970所以我可以这样做:let myAction = Action {callback()}和#selector(myAction.action)但我得到了“无法识别的选择器发送到实例” – victorsarda

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

}

这或许可以帮助你

+0

谢谢,但我得到了同样的错误:“#selector'的参数不能引用变量'objCallback'” – victorsarda

+0

你有没有在类中声明属性? –

+0

var objCallback:(() - >(Void))? – victorsarda