-2

I am using snackBar in my app, it's working fine in my application but I want to access the Custom Function from SnackBar Action Block.

I can try that it's showing

  • Instance member 'uncheck' cannot be used on type 'DetailsInfoViewController' did you mean to use a value of this type instead?

This is the code

let snackbar = TTGSnackbar(message: "Attendance Update !",
                           duration: .long,
                           actionText: "SEND",

                           actionBlock: {
                                (snackbar) in

                            uncheck()

                            print("snack bar Action")

})

func uncheck()
{

    print("snackbar uncheck function")
    snackbarCount -= 1
    checkSnackBar()
}

How can I access the custom function in snack Bar actionBlock inside?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
naga
  • 397
  • 2
  • 12
  • 26

1 Answers1

2

I'm assuming you've initialized snackbar inside a class func or something like that. From this place self represents your class DetailsInfoViewController while you need the object of this class. If you initialize this object before snackbar, you could write like this:

let controller: DetailsInfoViewController = ...
let snackbar = TTGSnackbar(message: "Attendance Update !", duration: .long, actionText: "SEND") { [weak controller] (snackbar) in
    controller?.uncheck()
    print("snack bar Action")
}

If it doesn't help you, please post the function code where you're creating snackbar.

UPD: With minimal changes of your code the following solution should work:

class DetailsInfoViewController: UIViewController {

    var snackbarCount = 0

    lazy var snackbar = TTGSnackbar(message: "Attendance Update !", duration: .long, actionText: "SEND") { (snackbar) in
        self.uncheck()
        print("snack bar Action")
    }

    func uncheck() {
        print("snackbar uncheck function")
        snackbarCount -= 1
        checkSnackBar()
    }

    func checkSnackBar() {}

}

You found really tricky situation when your class requires self inside closure as it initializes but this snackbar variable initializes before we have ready-to-use self variable. It means we need to initialize snackbar after self using lazy var.

But I strongly suggests you to not write code as you wrote it. The TTHSnackbar author's example is designed to be placed in a function when this snackbar really need to be shown. For example:

class DetailsInfoViewController: UIViewController {

    @IBAction func showSnackbarButtonTapped(_ sender: UIButton) {
        let snackbar = TTGSnackbar(message: "TTGSnackBar !", duration: .middle, actionText: "Action!") { (snackbar) in
            self.uncheck()
            print("snack bar Action")
        }
        snackbar.show()
    }

}

When it shows on the view it will be retained by this view.

UPD #2: To show only one Snackbar at a time you could write like this:

class DetailsInfoViewController: UIViewController {

    private weak var currentSnackbar: TTGSnackbar?

    @IBAction func showSnackbarButtonTapped(_ sender: UIButton) {
        if let snackbar = currentSnackbar {
            snackbar.dismiss()
            currentSnackbar = nil
        }

        let snackbar = TTGSnackbar(message: "TTGSnackBar !", duration: .middle, actionText: "Action!") { (snackbar) in
            self.uncheck()
            print("snack bar Action")
        }

        currentSnackbar = snackbar
        snackbar.show()
    }

}
Krin-San
  • 316
  • 1
  • 10
  • function code class DetailsInfoViewController: { let snackbar = TTGSnackbar(message: "Attendance Update !", duration: .long, actionText: "SEND", actionBlock: { (snackbar) in uncheck() print("snack bar Action") }) inside same class I'm calling func uncheck() function func uncheck() { print("snackbar uncheck function") snackbarCount -= 1 checkSnackBar() } – naga Jul 10 '17 at 18:23
  • lazy var snackbar = TTGSnackbar(message: "Attendance Update !", duration: .long, actionText: "SEND") { (snackbar) in self.uncheck() print("snack bar Action") } this can working fine thanks bro – naga Jul 11 '17 at 18:58
  • I'm using multiple check boxes in tableview I can try this one but multiple snack bars showing in table view @IBAction func showSnackbarButtonTapped(_ sender: UIButton) { let snackbar = TTGSnackbar(message: "TTGSnackBar !", duration: .middle, actionText: "Action!") { (snackbar) in self.uncheck() print("snack bar Action") } snackbar.show() } – naga Jul 11 '17 at 19:02
  • @nagarajukankanala, it could be done by keeping the weak reference to the last snackbar shown and hiding it before presenting a new one. I updated the answer to show it. – Krin-San Jul 12 '17 at 06:09