我有一个带有滑动手势识别器的表格视图控制器,只要用户向上滑动就会触发NSNotificationCenter.defaultCenter().postNotificationName("DuskTheme", object: nil)
。
在viewDidLoad()函数中,我有以下观察者:NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil)
调用函数dusk(notification: NSNotification)
,它改变当前视图控制器上元素的颜色(即主题)。
每当用户滑动时,我也希望更改导航栏的颜色,因此我将navigationController子类化,并将以下观察者添加到其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)
})
}
}
}
现在由于某种原因,只要刷了表视图控制器,app就会引发以下异常:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TestApp.customNavigationController dusk:]: unrecognized selector sent to instance 0x7939c910'
这个错误是由手势识别器引起的吗?它在继承导航控制器之前工作正常。更重要的是,什么是检测主题已更改并更改导航栏颜色的更好方法?
提前致谢!
答案 0 :(得分:2)
将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)
}
}