'Unable to get buttons currentTitle from UIStoryBoardSegue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Choose Theme" {
        if let themeName = (sender as? UIButton)?.currentTitle {
            if let theme = themes[themeName] {
                if let cvc = segue.destination as? ConcentrationViewController {
                    cvc.theme = theme
                }
            } else {
                print("something went wrong")
            }
        } else {
            print("couldnt get current title")
        }
    }
}

There is the code. I'm trying to get senders current title. I tried debugging and it prints "couldn't get current title", the value that I'm getting is nil.

What am I doing wrong?

Thanks in advance



Solution 1:[1]

Instead of using currentTitle, you might want to try titleLabel?.text. It seems to be more reliable. I got your program to work (tested in Xcode) using this change.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Quack E. Duck