我有一个带有滑动手势识别器的表视图控制器,每当用户向上滑动时,它就会触发NSNotificationCenter.defaultCenter().postNotificationName("DuskTheme", object: nil)
在viewDidLoad()函数中,我有以下观察者:NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil)它调用函数dusk(notification: NSNotification)来更改当前视图控制器(即主题)上元素的颜色。
我想改变我的导航栏的颜色,以及每当用户刷,所以我子类导航控制器,并添加了以下观察员到其viewDidLoad():NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil)以及dusk(notification: NSNotification)函数包含新的颜色导航栏,我从故事板链接。
这是我的自定义导航控制器类:

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)

            })
        }

    }

}

由于某种原因,每当刷表视图控制器时,应用程序就会抛出以下异常:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TestApp.customNavigationController dusk:]: unrecognized selector sent to instance 0x7939c910'

这个错误是由手势识别器引起的吗?在子类化导航控制器之前,它工作得很好。更重要的是,有什么更好的方法可以检测到主题已更改,并更改导航栏的颜色?
提前谢谢!

最佳答案

dusk()移到viewDidLoad()之外。它需要在最高层:

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)
    }
}

10-06 09:32