为什么我的 NSNotificationCenter 抛出异常?
Why is my NSNotificationCenter throwing an exception?
我有一个 table 视图控制器,它带有滑动手势识别器,只要用户向上滑动就会触发 NSNotificationCenter.defaultCenter().postNotificationName("DuskTheme", object: nil)
。
在 viewDidLoad() 函数中,我有以下观察者:NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil)
调用函数 dusk(notification: NSNotification)
更改当前视图控制器(即主题)上元素的颜色。
我也想在用户滑动时更改导航栏的颜色,因此我子class编辑了 navigationController 并将以下观察器添加到它的 viewDidLoad() 中:NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil)
作为dusk(notification: NSNotification)
函数包含我从 Storyboard 链接的导航栏的新颜色。
这是我的自定义导航控制器 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)
})
}
}
}
现在出于某种原因,每当滑动 table 视图控制器时,应用程序都会抛出以下异常:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TestApp.customNavigationController dusk:]: unrecognized selector sent to instance 0x7939c910'
这个错误是由手势识别器引起的吗?它在 subclass 导航控制器之前工作正常。更重要的是,检测主题已更改并更改导航栏颜色的更好方法是什么?
提前致谢!
将 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)
}
}
我有一个 table 视图控制器,它带有滑动手势识别器,只要用户向上滑动就会触发 NSNotificationCenter.defaultCenter().postNotificationName("DuskTheme", object: nil)
。
在 viewDidLoad() 函数中,我有以下观察者:NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil)
调用函数 dusk(notification: NSNotification)
更改当前视图控制器(即主题)上元素的颜色。
我也想在用户滑动时更改导航栏的颜色,因此我子class编辑了 navigationController 并将以下观察器添加到它的 viewDidLoad() 中:NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil)
作为dusk(notification: NSNotification)
函数包含我从 Storyboard 链接的导航栏的新颜色。
这是我的自定义导航控制器 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)
})
}
}
}
现在出于某种原因,每当滑动 table 视图控制器时,应用程序都会抛出以下异常:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TestApp.customNavigationController dusk:]: unrecognized selector sent to instance 0x7939c910'
这个错误是由手势识别器引起的吗?它在 subclass 导航控制器之前工作正常。更重要的是,检测主题已更改并更改导航栏颜色的更好方法是什么?
提前致谢!
将 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)
}
}