2017-02-22 20 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