0

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!

cyril
  • 3,020
  • 6
  • 36
  • 61
  • If `dusk()` takes no parameters, change your selector to `dusk`, get rid of `:`. – vacawama Jun 06 '15 at 14:28
  • @vacawama fixed the above code – cyril Jun 06 '15 at 14:30
  • Is `func dusk(notification: NSNotification)` a top level method inside of `customNavigationController` (i.e. it isn't inside of another function such as `viewDidLoad`)? – vacawama Jun 06 '15 at 14:35
  • How is your `customNavigationController` created? In the Storyboard? Did you change the class of the `NavigationController` to use your custom class? – vacawama Jun 06 '15 at 14:39
  • Updated the post with my custom navigation controller. Yes, no and Yes for the other questions @vacawama – cyril Jun 06 '15 at 14:47

1 Answers1

2

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)
    }
}
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • How is it that putting it at the top fixed it? How does the order affect the way an app runs? I'm still new to all this and self-taught, would you mind explaining it to me? – cyril Jun 06 '15 at 14:59
  • 2
    It doesn't matter if `dusk()` is above or below `viewDidLoad`, but it needs to be at the same nesting level as `viewDidLoad`. The selector is called on the `customNavigatorController` instance, so the function needs to be a top level function and not nested inside of another function. – vacawama Jun 06 '15 at 15:01