我写了一个更改导航栏颜色的函数:
func changeNavigationBarTitleAndAppearance(title title:String) {
switch (title) {
case "active":
self.title = "Einsatzbereit".uppercaseString
if let navController = navigationController {
navController.navigationBar.barTintColor = UIColor(red: 73.0/255.0, green: 181.0/255.0, blue: 177.0/255.0, alpha: 1.0)
}
case "inactive":
self.title = "Inaktiv".uppercaseString
if let navController = navigationController {
navController.navigationBar.barTintColor = UIColor(red: 40.0/255.0, green: 51.0/255.0, blue: 57.0/255.0, alpha: 1.0)
}
case "dispatch":
self.title = "Alarmierung".uppercaseString
if let navController = navigationController {
navController.navigationBar.barTintColor = UIColor(red: 208.0/255.0, green: 2.0/255.0, blue: 27.0/255.0, alpha: 1.0)
}
default:
return
}
func dispatchReceived(notification: NSNotification) {
print(notification.name)
switch (notification.name) {
case "DISPATCH_RECEIVED":
self.changeNavigationBarTitleAndAppearance(title: "dispatch")
case "DISPATCH_ACCEPTED":
self.changeNavigationBarTitleAndAppearance(title: "active")
case "DISPATCH_DECLINED":
self.changeNavigationBarTitleAndAppearance(title: "active")
default:
return
}
}
我发送的第一个通知是:“DISPATCH_RECEIVED”。正如预期的那样,导航栏色调变为红色,如函数中所述。但是当我发送消息“DISPATCH_DECLINED”时,函数会按预期调用以更改颜色,但不会再发生任何事情而且一切都会保持红色。
视图控制器观察:
self.notificationCenter.addObserver(self, selector: "dispatchReceived:", name: "DISPATCH_RECEIVED", object: nil)
self.notificationCenter.addObserver(self, selector: "dispatchReceived:", name: "DISPATCH_DECLINED", object: nil)
并调用与预期相关的方法。
当我发送“DISPATCH changeNavigationBarTitleAndAppearance(title: "dispatch")
但是当我发送第二个通知时