cyril
cyril

Reputation: 3005

Why is my NSNotificationCenter throwing an exception?

I have a table view controller with a swipe gesture recognizer that fires NSNotificationCenter.defaultCenter().postNotificationName("DuskTheme", object: nil) whenever the user swipes up.

In the viewDidLoad() function, I have the following observer: NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil) which calls the function dusk(notification: NSNotification) that changes the color of the elements on the current view controller (i.e. a theme).

I wanted to change the color of my navigation bar as well whenever the user swipes and so I subclassed the navigationController and added the following observer to its viewDidLoad(): NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil) as well as the dusk(notification: NSNotification)function containing the new color for the navigation bar which I linked from Storyboard.

Here is my custom nav controller class:

class customNavigationController: UINavigationController {
    @IBOutlet weak var featuredNavBar = ThemeManager.navigationbar

    override func viewDidLoad() {
        super.viewDidLoad()
        //Adding a theme notification observer
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil)

        func dusk(notification: NSNotification) {
            UIView.animateWithDuration(1, animations: {
                UIApplication.sharedApplication().statusBarStyle = .LightContent
                self.featuredNavBar?.barTintColor = UIColor(red: 69/255, green: 69/255, blue: 69/255, alpha: 1)

            })
        }

    }

}

Now for some reason whenever the table view controller is swiped the app throws the following exception:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TestApp.customNavigationController dusk:]: unrecognized selector sent to instance 0x7939c910'

Is this error being caused by the gesture recognizer? It worked fine before subclassing the navigation controller. More importantly what would be a better way of detecting that the theme has been changed and changing the navigation bar color?

Thanks in advance!

Upvotes: 0

Views: 663

Answers (1)

vacawama
vacawama

Reputation: 154721

Move dusk() outside of viewDidLoad(). It needs to be at the top level:

class customNavigationController: UINavigationController {
    @IBOutlet weak var featuredNavBar = ThemeManager.navigationbar

    func dusk(notification: NSNotification) {
        UIView.animateWithDuration(1, animations: {
            UIApplication.sharedApplication().statusBarStyle = .LightContent
            self.featuredNavBar?.barTintColor = UIColor(red: 69/255, green: 69/255, blue: 69/255, alpha: 1)

        })
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //Adding a theme notification observer
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil)
    }
}

Upvotes: 2

Related Questions